VioletaBabel

35. Terrain, nav mash 본문

BCA/4. Unity
35. Terrain, nav mash
Beabletoet 2018. 5. 10. 16:42

Terrain으로 길을 깔고 장애물 등을 배치한 후 Window의 Navigation을 누른다.


Terrain에는 Navigation-Object를 누른 후 Navigation Static과 Generate Offmeshlinks를 체크하고 Navigation Area를 Walkable로 바꾼다.


그 외 장애물에는 navigation Static만 체크하고 Navigation Area를 Not Walkable로 한다.


계단이 있는 곳이나 지나갈 수 있는 장애물(다리 등)은 Navigation Static만 체크하고 Navigation Area를 Walkable로 한다.


그리고 계단 등도 지나갈 수 있도록 Bake 항목의 Step Height는 0.75로 한다.


나중에 점프와 추락을 만들기 위해 Bake 항목의 Drop Height와 Jump Distance는 미리 10으로 올려둔다.


그리고 Bake를 누른다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//CharacterMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.AI;
 
public class CharacterMove : MonoBehaviour
{
    Animator CharAnim;
    NavMeshAgent agent;
    bool[] arrow = new bool[4] { falsefalsefalsefalse }; // wdsa 순
    float nowSpeed = 0;
    // Use this for initialization
    void Start()
    {
        CharAnim = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            CharAnim.SetBool("Jump_b"true);
        if (Input.GetKeyUp(KeyCode.Space))
            CharAnim.SetBool("Jump_b"false);
 
        //if (Input.GetKeyDown(KeyCode.W))
        //{
        //    CharAnim.SetBool("Static_b", false);
        //    CharAnim.SetFloat("Speed_f", 1f);
        //}
        //if (Input.GetKeyUp(KeyCode.W))
        //{
        //    CharAnim.SetBool("Static_b", true);
        //    CharAnim.SetFloat("Speed_f", 0f);
        //}
        //if (Input.GetKey(KeyCode.A))
        //{
        //    transform.Rotate(Vector3.down * Time.deltaTime * 20f);
        //}
        //if (Input.GetKey(KeyCode.D))
        //{
        //    transform.Rotate(Vector3.up * Time.deltaTime * 20f);
        //} // 1인칭스러운 뷰
 
        if (Input.GetKeyDown(KeyCode.W))
        {
            arrow[0= true;
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            arrow[0= false;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            arrow[3= true;
        }
        if (Input.GetKeyUp(KeyCode.A))
        {
            arrow[3= false;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            arrow[2= true;
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            arrow[2= false;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            arrow[1= true;
        }
        if (Input.GetKeyUp(KeyCode.D))
        {
            arrow[1= false;
        }
 
        if ((arrow[0&& arrow[2]) || (arrow[1&& arrow[3])) ;
        else if (arrow[0&& arrow[1])
        {//오른쪽위
            transform.DORotate(new Vector3(0450), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(101);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[0&& arrow[3])
        {//왼쪽위
            transform.DORotate(new Vector3(0-450), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(-101);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[1&& arrow[2])
        {//오른쪽아래
            transform.DORotate(new Vector3(01350), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(10-1);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[2&& arrow[3])
        {//왼쪽아래
            transform.DORotate(new Vector3(02250), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(-10-1);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[0])
        {
            transform.DORotate(new Vector3(000), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(001);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[1])
        {
            transform.DORotate(new Vector3(0900), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(100);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[2])
        {
            transform.DORotate(new Vector3(01800), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(00-1);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            agent.ResetPath();
            transform.position = charPos;
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else if (arrow[3])
        {
            transform.DORotate(new Vector3(02700), 0.5f);
            if (nowSpeed < 0.2f)
                nowSpeed += 0.002f;
            Vector3 charPos = transform.position;
            Vector3 arrowPos = new Vector3(-100);
            arrowPos.Normalize();
            charPos += arrowPos * nowSpeed;
            transform.position = charPos;
            //agent.destination = charPos;
            agent.ResetPath();
            CharAnim.SetFloat("Speed_f", 1f);
        }
        else
        {
            //CharAnim.SetBool("Static_b", true);
            nowSpeed = 0;
            CharAnim.SetFloat("Speed_f", 0f);
            print(agent.destination);
        }
        Vector3 pos = transform.position;
        //if (pos.y > 0.5f || pos.y < 0.5f)
        //{
        //    pos.y = 0;
        //    transform.position = pos;
        //}
        if (Input.GetMouseButton(0))
        {
            //CharAnim.SetInteger("WeaponType_int", 12);
 
            Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit[] hit = Physics.RaycastAll(r);
            foreach (RaycastHit h in hit)
            {
                if (h.transform.name.Contains("Terrain"))
                {
                    Vector3 destPos = h.point;
                    agent.destination = destPos;
                    print(agent.speed);
                    
                }
            }
        }
        if(Vector3.Distance(agent.destination, transform.position) > 0.5f)
        {
            CharAnim.SetFloat("Speed_f", 1f);
        }
 
 
 
        //else if (CharAnim.GetInteger("WeaponType_int").Equals(12))
        //{
        //    CharAnim.SetInteger("WeaponType_int", 0);
        //}
    }
 
    //void run()
    //{
    //    //if (CharAnim.GetBool("Static_b"))
    //    {
    //        //CharAnim.SetBool("Static_b", false);
    //        CharAnim.SetFloat("Speed_f", 1f);
    //    }
    //}
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Player_CameraAttach.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class Player_CameraAttach : MonoBehaviour
{
 
    public GameObject mainCamera = null;
    public Camera mainCam;
    // Use this for initialization
    void Start()
    {
        mainCam = mainCamera.GetComponent<Camera>();
    }
 
    // Update is called once per frame
    void Update()
    {
        //mainCamera.transform.position = transform.position;
        //mainCamera.transform.Translate(new Vector3(0, 50, -100));
        //mainCamera.transform.rotation = transform.rotation; //등 뒤에 붙일 때
 
        Vector3 a = transform.position;
        a.y += 10;
        a.z -= 10;
        //mainCamera.transform.position = a;
        mainCamera.transform.DOMove(a, 1.5f);
 
 
        float fov = Input.GetAxis("Mouse ScrollWheel");
        if (!fov.Equals(0))
        {//zoom
            mainCam.fieldOfView -= fov * 10f;
        }
    }
}
 
cs




위 두 코드는 모두 Player 캐릭터에게 붙이고, 하이에라키의 메인 카메라를 Player_CameraAttach.cs의 Main Camera 항목에 넣는다.



wasd로 움직이기도 하고, 마우스로 클릭 시 길찾기를 하여 이동한다.


그리고 장애물들의 인스펙터에 Nav Mesh Obstacle을 넣고 Carve에 체크하면 클릭으로 길찾기를 할 때 해당 장애물은 자동으로 우회한다.

'BCA > 4. Unity' 카테고리의 다른 글

39. 미니맵  (0) 2018.06.07
38. 유니티에 채팅용 서버 기본 연결  (0) 2018.06.05
34. 3D plane에서 cube 이동  (0) 2018.05.09
32. 타워 디펜스게임  (0) 2018.05.03
31. 연출용 툴 이용해 유니티에서 연출  (0) 2018.04.27
Comments