Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NPCPatrol : MonoBehaviour {
  6.  
  7. public Transform[] WayPoints;
  8. const float m_MaxSpeed=3;
  9. const float m_SpeedDampTime=.25f;
  10. const float m_DirectionDampTime=.25f;
  11. const float m_ThresholdDistance=1.5f;
  12. int m_WayPointIndex=0;
  13. Animator m_Animator=null;
  14. void Start () {
  15. m_Animator = GetComponents<Animator> ();
  16. }
  17.  
  18. void Update () {
  19. if (WayPoints.Length < 0)
  20. return;
  21. Transform target = WayPoints [m_WayPointIndex];
  22. if (target == null)
  23. return;
  24. if (Vector3.Distance (target.position, m_Animator.rootPosition > m_ThresholdDistance)) {
  25. m_Animator.SetFloat ("Speed", m_MaxSpeed, m_SpeedDampTime, Time.deltaTime);
  26. Vector3 curentDir = m_Animator.rootRotation * Vector3.forward;
  27. Vector3 wanteDir = (target.position - m_Animator.rootPosition).normalized;
  28. if (Vector3.Dot (curentDir, wanteDir) > 0) {
  29. m_Animator.SetFloat ("Direction", Vector3.Cross (curentDir, wanteDir).y, m_DirectionDampTime, Time.deltaTime);
  30.  
  31. } else {
  32. m_Animator.SetFloat ("Direction", Vector3.Cross (curentDir.wantedDir).y > 0 ? 1 : -1, m_DirectionDampTime, Time.deltaTime);
  33. }
  34. } else {
  35. m_Animator.SetFloat ("Speed",0,m_SpeedDampTime,Time.deltaTime);
  36. m_WayPointIndex = (m_WayPointIndex + 1) % WayPoints.Length;
  37.  
  38. }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement