MovePath.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using UnityEngine;
  2. using System.Collections;
  3. [System.Serializable]
  4. public class MovePath : MonoBehaviour {
  5. [SerializeField]
  6. public Vector3 startPos;
  7. [SerializeField]
  8. public Vector3 finishPos;
  9. [SerializeField]
  10. public int w;
  11. [SerializeField]
  12. public int targetPoint;
  13. [SerializeField]
  14. public int targetPointsTotal;
  15. [SerializeField]
  16. public string animName;
  17. [SerializeField]
  18. public float walkSpeed;
  19. [SerializeField]
  20. public float runSpeed;
  21. [SerializeField]
  22. public bool loop;
  23. [SerializeField]
  24. public bool forward;
  25. [SerializeField]
  26. public GameObject walkPath;
  27. [HideInInspector] public float randXFinish;
  28. [HideInInspector] public float randZFinish;
  29. [SerializeField] [Tooltip("Set your animation speed / Установить свою скорость анимации?")] private bool _overrideDefaultAnimationMultiplier;
  30. [SerializeField] [Tooltip("Speed animation walking / Скорость анимации ходьбы")] private float _customWalkAnimationMultiplier = 1.0f;
  31. [SerializeField] [Tooltip("Running animation speed / Скорость анимации бега")] private float _customRunAnimationMultiplier = 1.0f;
  32. public void InitializeAnimation(bool overrideAnimation, float walk, float run)
  33. {
  34. _overrideDefaultAnimationMultiplier = overrideAnimation;
  35. _customWalkAnimationMultiplier = walk;
  36. _customRunAnimationMultiplier = run;
  37. }
  38. public void MyStart(int _w, int _i, string anim, bool _loop, bool _forward, float _walkSpeed, float _runSpeed)
  39. {
  40. forward = _forward;
  41. walkSpeed = _walkSpeed;
  42. runSpeed = _runSpeed;
  43. var _WalkPath = walkPath.GetComponent<WalkPath>();
  44. w = _w;
  45. targetPointsTotal = _WalkPath.getPointsTotal(0) - 2;
  46. loop = _loop;
  47. animName = anim;
  48. if(loop)
  49. {
  50. if(_i < targetPointsTotal && _i > 0)
  51. {
  52. if(forward)
  53. {
  54. targetPoint = _i + 1;
  55. finishPos = _WalkPath.getNextPoint(w, _i+1);
  56. }
  57. else
  58. {
  59. targetPoint = _i;
  60. finishPos = _WalkPath.getNextPoint(w, _i);
  61. }
  62. }
  63. else
  64. {
  65. if(forward)
  66. {
  67. targetPoint = 1;
  68. finishPos = _WalkPath.getNextPoint(w, 1);
  69. }
  70. else
  71. {
  72. targetPoint = targetPointsTotal;
  73. finishPos = _WalkPath.getNextPoint(w, targetPointsTotal);
  74. }
  75. }
  76. }
  77. else
  78. {
  79. if(forward)
  80. {
  81. targetPoint = _i + 1;
  82. finishPos = _WalkPath.getNextPoint(w, _i+1);
  83. }
  84. else
  85. {
  86. targetPoint = _i;
  87. finishPos = _WalkPath.getNextPoint(w, _i);
  88. }
  89. }
  90. }
  91. public void SetLookPosition()
  92. {
  93. Vector3 targetPos = new Vector3(finishPos.x, transform.position.y, finishPos.z);
  94. transform.LookAt(targetPos);
  95. }
  96. void Start()
  97. {
  98. Animator animator = GetComponent<Animator>();
  99. animator.CrossFade(animName, 0.1f, 0, Random.Range(0.0f, 1.0f));
  100. if (animName == "walk")
  101. {
  102. if (_overrideDefaultAnimationMultiplier)
  103. {
  104. animator.speed = walkSpeed * _customWalkAnimationMultiplier;
  105. }
  106. else
  107. {
  108. animator.speed = walkSpeed * 1.2f;
  109. }
  110. }
  111. else if (animName == "run")
  112. {
  113. if (_overrideDefaultAnimationMultiplier)
  114. {
  115. animator.speed = runSpeed * _customRunAnimationMultiplier;
  116. }
  117. else
  118. {
  119. animator.speed = runSpeed / 3;
  120. }
  121. }
  122. }
  123. void Update ()
  124. {
  125. RaycastHit hit;
  126. if(Physics.Raycast(transform.position + new Vector3(0, 2, 0), -transform.up, out hit))
  127. {
  128. finishPos.y = hit.point.y;
  129. transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
  130. }
  131. //Vector3 targetPos = new Vector3(finishPos.x, transform.position.y, finishPos.z);
  132. Vector3 randFinishPos = new Vector3(finishPos.x + randXFinish, finishPos.y, finishPos.z + randZFinish);
  133. Vector3 targetPos = new Vector3(randFinishPos.x, transform.position.y, randFinishPos.z);
  134. var _WalkPath = walkPath.GetComponent<WalkPath>();
  135. var richPointDistance = Vector3.Distance(Vector3.ProjectOnPlane(transform.position, Vector3.up), Vector3.ProjectOnPlane(randFinishPos, Vector3.up));
  136. if (richPointDistance < 0.2f && animName == "walk" && ((loop) || (!loop && targetPoint > 0 && targetPoint < targetPointsTotal)))
  137. {
  138. if(forward)
  139. {
  140. if(targetPoint < targetPointsTotal)
  141. targetPos = _WalkPath.getNextPoint(w, targetPoint + 1);
  142. else
  143. targetPos = _WalkPath.getNextPoint(w, 0);
  144. targetPos.y = transform.position.y;
  145. }
  146. else
  147. {
  148. if(targetPoint > 0)
  149. targetPos = _WalkPath.getNextPoint(w, targetPoint - 1);
  150. else
  151. targetPos = _WalkPath.getNextPoint(w, targetPointsTotal);
  152. targetPos.y = transform.position.y;
  153. }
  154. }
  155. if(richPointDistance < 0.5f && animName == "run" && ((loop) || (!loop && targetPoint > 0 && targetPoint < targetPointsTotal)))
  156. {
  157. if(forward)
  158. {
  159. if(targetPoint < targetPointsTotal)
  160. targetPos = _WalkPath.getNextPoint(w, targetPoint + 1);
  161. else
  162. targetPos = _WalkPath.getNextPoint(w, 0);
  163. targetPos.y = transform.position.y;
  164. }
  165. else
  166. {
  167. if(targetPoint > 0)
  168. targetPos = _WalkPath.getNextPoint(w, targetPoint - 1);
  169. else
  170. targetPos = _WalkPath.getNextPoint(w, targetPointsTotal);
  171. targetPos.y = transform.position.y;
  172. }
  173. }
  174. Vector3 targetVector = targetPos - transform.position;
  175. if(targetVector != Vector3.zero)
  176. {
  177. /*Quaternion look = Quaternion.identity;
  178. if(animName == "walk")
  179. look = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(targetVector), Time.deltaTime * 3f * moveSpeed);
  180. else if(animName == "run")
  181. look = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(targetVector), Time.deltaTime * 1.3f* moveSpeed);
  182. transform.rotation = look;*/
  183. Vector3 newDir = Vector3.zero;
  184. newDir = Vector3.RotateTowards(transform.forward, targetVector, 2 * Time.deltaTime, 0.0f);
  185. transform.rotation = Quaternion.LookRotation(newDir);
  186. }
  187. if (richPointDistance > 1)
  188. {
  189. if (Time.deltaTime > 0)
  190. {
  191. //transform.position += transform.forward * moveSpeed * Time.deltaTime;
  192. transform.position = Vector3.MoveTowards(transform.position, finishPos, Time.deltaTime * 1.0f * (animName == "walk" ? walkSpeed : runSpeed));
  193. }
  194. }
  195. else if (richPointDistance <= 1 && forward){
  196. if(targetPoint != targetPointsTotal)
  197. {
  198. targetPoint++;
  199. finishPos = _WalkPath.getNextPoint(w, targetPoint);
  200. }
  201. else if(targetPoint == targetPointsTotal)
  202. {
  203. if(loop)
  204. {
  205. finishPos = _WalkPath.getStartPoint(w);
  206. targetPoint = 0;
  207. }
  208. else
  209. {
  210. _WalkPath.SpawnOnePeople(w, forward, walkSpeed, runSpeed);
  211. Destroy(gameObject);
  212. }
  213. }
  214. }
  215. else if (richPointDistance <= 1 && !forward){
  216. if(targetPoint > 0)
  217. {
  218. targetPoint--;
  219. finishPos = _WalkPath.getNextPoint(w, targetPoint);
  220. }
  221. else if(targetPoint == 0)
  222. {
  223. if(loop)
  224. {
  225. finishPos = _WalkPath.getNextPoint(w, targetPointsTotal);
  226. targetPoint = targetPointsTotal;
  227. }
  228. else
  229. {
  230. _WalkPath.SpawnOnePeople(w, forward, walkSpeed, runSpeed);
  231. Destroy(gameObject);
  232. }
  233. }
  234. }
  235. }
  236. }