|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using UnityEngine;
|
5 | 5 |
|
6 |
| -[RequireComponent(typeof(Rigidbody))] |
| 6 | +//=================================================================================================== |
| 7 | +// |
| 8 | +// PATH FOLLWER CLASS |
| 9 | +// |
| 10 | +// Script to follow the path created by "Path Generator" class |
| 11 | +// Path Generator가 만든 Path를 따라가는 기능 |
| 12 | +// |
| 13 | +//--------------------------------------------------------------------------------------------------- |
| 14 | +// 2020.08.30 _ KimYC1223 |
| 15 | +//=================================================================================================== |
7 | 16 |
|
8 |
| -[System.Serializable] |
9 |
| -public class EndEvent : UnityEngine.Events.UnityEvent<GameObject> { } |
| 17 | +[RequireComponent(typeof(Rigidbody))] |
10 | 18 |
|
11 | 19 | public class PathFollower : MonoBehaviour
|
12 | 20 | {
|
13 |
| - public EventArgs endEvent; |
14 |
| - public PathGenerator path; |
15 |
| - public float speed = 100f; |
16 |
| - public float turningSpeed = 10f; |
17 |
| - public bool isLoop = false; |
18 |
| - public bool isMove = true; |
| 21 | + [System.Serializable] |
| 22 | + public class EndEvent : UnityEngine.Events.UnityEvent { } |
| 23 | + |
| 24 | + [SerializeField] |
| 25 | + public EndEvent endEvent; // method to run when this is done moving |
19 | 26 |
|
20 |
| - private Rigidbody TargetRigidbody; |
21 |
| - private GameObject Target; |
22 |
| - private GameObject NextAngle; |
23 |
| - private int AngleStep = 1; |
| 27 | + public PathGenerator path; // choose the path to move |
| 28 | + public float speed = 100f; // move speed |
| 29 | + public float turningSpeed = 10f; // rotation speed |
| 30 | + public bool isLoop = false; // does it move repeatedly? |
| 31 | + public bool isMove = true; // is this moving now? |
24 | 32 |
|
25 |
| - |
| 33 | + private Rigidbody targetRigidbody; // the rigidbody of the object to move |
| 34 | + private GameObject target; // object to move; |
| 35 | + private GameObject nextPath; // the direction the obejct will move |
| 36 | + private int pathIndex = 1; // the path index the object will move |
| 37 | + private float distanceThreshold = 0.2f; // distance threshold |
26 | 38 |
|
| 39 | + //=============================================================================================== |
| 40 | + // Start method |
| 41 | + //----------------------------------------------------------------------------------------------- |
| 42 | + // init variable & position |
| 43 | + // 각종 변수와 position 초기화 |
| 44 | + //=============================================================================================== |
27 | 45 | void Start()
|
28 | 46 | {
|
29 |
| - TargetRigidbody = GetComponent<Rigidbody>(); |
30 |
| - if (path == null) { |
31 |
| - Debug.LogError("경로가 없음"); |
32 |
| - } |
33 |
| - Target = this.gameObject; |
34 |
| - NextAngle = path.PathList[1]; |
| 47 | + targetRigidbody = GetComponent<Rigidbody>(); |
| 48 | + if (path == null) |
| 49 | + Debug.LogError("no path\n경로가 없음"); |
| 50 | + target = this.gameObject; |
| 51 | + nextPath = path.PathList[1]; |
35 | 52 | this.transform.position = path.PathList[0].transform.position;
|
36 |
| - if (NextAngle == null) |
37 |
| - Debug.Log("gg"); |
38 | 53 | }
|
39 |
| - |
40 |
| - // Update is called once per frame |
| 54 | + |
| 55 | + //=============================================================================================== |
| 56 | + // Fixed update method |
| 57 | + //----------------------------------------------------------------------------------------------- |
| 58 | + // set velocity & direction, and calculate distance |
| 59 | + // 속도와 방향 설정 후 거리 계산 |
| 60 | + //=============================================================================================== |
41 | 61 | void FixedUpdate()
|
42 | 62 | {
|
| 63 | + //=========================================================================================== |
| 64 | + // If it is not moving, stop object and return |
| 65 | + // 움직이지 않는다면, 물체를 멈추고 종료 |
| 66 | + //=========================================================================================== |
43 | 67 | if (!isMove) {
|
44 |
| - TargetRigidbody.velocity = new Vector3(0, 0, 0); |
| 68 | + targetRigidbody.velocity = new Vector3(0, 0, 0); // Stop |
45 | 69 | return;
|
46 | 70 | }
|
47 |
| - // ===================================================================== |
48 |
| - // 자동차가 가이드를 바라보게하는 기능 |
49 |
| - // ===================================================================== |
50 |
| - Vector3 offset = NextAngle.transform.position - Target.transform.position; |
| 71 | + |
| 72 | + //=========================================================================================== |
| 73 | + // Function to make objects look at the next path |
| 74 | + // 물체가 다음 Path를 바라보게하는 기능 |
| 75 | + //=========================================================================================== |
| 76 | + Vector3 offset = nextPath.transform.position - target.transform.position; |
51 | 77 | offset.Normalize();
|
52 | 78 | Quaternion q = Quaternion.LookRotation(offset);
|
53 |
| - TargetRigidbody.rotation = |
54 |
| - Quaternion.Slerp(TargetRigidbody.rotation, |
| 79 | + targetRigidbody.rotation = |
| 80 | + Quaternion.Slerp(targetRigidbody.rotation, |
55 | 81 | q, turningSpeed * Time.deltaTime);
|
56 | 82 |
|
57 |
| - |
58 |
| - // ===================================================================== |
59 |
| - // 자동차가 가이드를 따라가게 하는 기능 |
60 |
| - // ===================================================================== |
| 83 | + //=========================================================================================== |
| 84 | + // Function to make objects follow a path |
| 85 | + // 물체가 path를 따라가게 하는 기능 |
| 86 | + //=========================================================================================== |
61 | 87 | offset.Normalize();
|
62 |
| - TargetRigidbody.velocity = offset * speed * Time.deltaTime; |
| 88 | + targetRigidbody.velocity = offset * speed * Time.deltaTime; |
| 89 | + |
| 90 | + // calculate distance between object and next path |
| 91 | + // 물체와 next path 경로 사이의 거리 계산 |
| 92 | + float Distance = Vector3.Distance(nextPath.transform.position, |
| 93 | + target.transform.position); |
63 | 94 |
|
64 |
| - // 두 거리 계산 |
65 |
| - float Distance = Vector3.Distance(NextAngle.transform.position, |
66 |
| - Target.transform.position); |
| 95 | + //=========================================================================================== |
| 96 | + // If it is close enough to the next path |
| 97 | + // next path에 충분히 가까워졌을 경우 |
| 98 | + //=========================================================================================== |
| 99 | + if (Distance < distanceThreshold) { |
67 | 100 |
|
68 |
| - // ===================================================================== |
69 |
| - // 가이드에 가까워 졌을 경우 |
70 |
| - // ===================================================================== |
71 |
| - if (Distance < 0.2f) { |
72 |
| - |
73 |
| - if(AngleStep >= path.PathList.Count) { |
| 101 | + if (pathIndex < path.PathList.Count) { |
| 102 | + //======================================================================================= |
| 103 | + // If the end of the path list is not reached, set the next path by increase path Index |
| 104 | + // path 리스트의 끝에 도달하지 못했다면, path Index ++ 를 통해 next path 설정 |
| 105 | + //======================================================================================= |
| 106 | + nextPath = path.PathList[pathIndex++]; |
| 107 | + } else { |
| 108 | + //=================================================================================== |
| 109 | + // If the object reached end of the path list, |
| 110 | + // path 리스트 끝에 도달했다면, 즉, 최종 목적지에 도달했을때 |
| 111 | + //=================================================================================== |
74 | 112 | if (path.isClosed) {
|
75 |
| - NextAngle = path.PathList[0]; |
76 |
| - AngleStep = 0; |
| 113 | + //=============================================================================== |
| 114 | + // If current path is closed path, back to zero of the path list |
| 115 | + // 현재 path가 닫힌 경로이면, 다시 pathList[0]을 향해 전진 |
| 116 | + //=============================================================================== |
| 117 | + nextPath = path.PathList[0]; |
| 118 | + pathIndex = 0; |
77 | 119 | } else {
|
78 |
| - if(isLoop) { |
79 |
| - NextAngle = path.PathList[1]; |
80 |
| - AngleStep = 1; |
| 120 | + //=============================================================================== |
| 121 | + // If current path is open path, |
| 122 | + // 현재 path가 열린 경로이면, |
| 123 | + //=============================================================================== |
| 124 | + if (isLoop) { |
| 125 | + //=========================================================================== |
| 126 | + // and object move repeatedly reinit position & value; |
| 127 | + // 그리고 물체가 반복적으로 움직인다면, position과 변수 다시 초기화 |
| 128 | + //=========================================================================== |
| 129 | + nextPath = path.PathList[1]; |
| 130 | + pathIndex = 1; |
81 | 131 | this.transform.position = path.PathList[0].transform.position;
|
82 |
| - Target.transform.LookAt(path.PathList[1].transform); |
| 132 | + target.transform.LookAt(path.PathList[1].transform); |
| 133 | + // If endEvent isn't null, run method. |
| 134 | + // endEvent가 null이 아니면, method를 실행 |
| 135 | + if (endEvent != null) |
| 136 | + endEvent.Invoke(); |
83 | 137 | } else {
|
| 138 | + //========================================================================== |
| 139 | + // If object move once, Stop move and if endEvent isn't null, run method. |
| 140 | + // 물체가 한번만 움직이면 멈추고, endEvent!=null이 아니면, method를 실행 |
| 141 | + //========================================================================== |
84 | 142 | StopFollow();
|
| 143 | + if (endEvent != null) |
| 144 | + endEvent.Invoke(); |
85 | 145 | }
|
86 | 146 | }
|
87 |
| - } else { |
88 |
| - NextAngle = path.PathList[AngleStep++]; |
89 | 147 | }
|
90 | 148 | }
|
91 | 149 | }
|
92 | 150 |
|
93 |
| - |
| 151 | + //=============================================================================================== |
| 152 | + // Stop Follow method |
| 153 | + //----------------------------------------------------------------------------------------------- |
| 154 | + // stop move by set isMove false |
| 155 | + // 정지 |
| 156 | + //=============================================================================================== |
94 | 157 | public void StopFollow() {
|
95 | 158 | isMove = false;
|
96 | 159 | }
|
97 | 160 |
|
| 161 | + //=============================================================================================== |
| 162 | + // Start Follow method |
| 163 | + //----------------------------------------------------------------------------------------------- |
| 164 | + // Start move by set isMove true |
| 165 | + // 움직임 |
| 166 | + //=============================================================================================== |
98 | 167 | public void StartFollow() {
|
99 | 168 | isMove = true;
|
100 | 169 | }
|
|
0 commit comments