Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PedestrianObject : MonoBehaviour
  5. {
  6. public PedestrianSystem.ObjectFrequency m_assetFrequency = PedestrianSystem.ObjectFrequency.HIGH;
  7.  
  8. public PedestrianNode m_currentNode = null; // the current node that the pedestrian object will travel to
  9. public float m_minSpeed = 1.0f; // the minimum speed that the pedestrian object will travel at
  10. public float m_maxSpeed = 1.0f; // the maximum speed that the pedestrian object will travel at
  11. [Range(0.0f, 1.0f)]
  12. public float m_percentageOfSpeedToUse = 1.0f; // 100% uses the entire values from min to max speed. 34% would only use from min to 35% of max speed. Set this to make objects use a certain amount of the full min / max speed value (currently up to 34% allows for walking animation only)
  13. protected float m_speed = 0.0f; // the speed that the pedestrian object will travel at
  14. private float m_speedStoredWhileWaiting = 0.0f; // used to record a speed when a node is telling the object to wait ( m_waitAtNode = true )
  15. protected float m_currentSpeed = 0.0f; // this is always our current speed
  16. public float m_startMovingDelayMin = 0.0f; // this is the minium delay the object will wait before it starts moving
  17. public float m_startMovingDelayMax = 0.0f; // this is the maxium delay the object will wait before it starts moving
  18. public float m_rotationSpeed = 3.5f; // the speed at which we will rotaion
  19. public bool m_onlyRotYAxis = true; // set to true this means the object will stand upright always
  20. public float m_nodeThreshold = 0.1f; // how close the pedestrian object needs to get to the m_currentNode before it moves on to another node
  21.  
  22. public int m_nodeVisitThreshold = 2; // this is the amount of nodes to remember we have visited so we don't visit the same node again within this threshold
  23. private PedestrianNode[] m_prevPedestrianNodes;
  24. private int m_nodeVisitIndex = 0;
  25.  
  26. public PathingStatus m_pathingStatus = PathingStatus.RANDOM; // this determines how the pedestrian object will traverse through the pathing nodes
  27. public int m_pathingIndex = 0; // if the m_pathingStatus is set to INDEX, then the pedestrian object will try to use this index position for the m_nodes associated in the PedestrianNode.cs object
  28. public Vector3 m_offsetPosVal = Vector3.zero; // the amount to offset the position of this object
  29. public bool m_lookAtNode = true; // set this to true if you want the objects forward direction to face the node it is currently going towards
  30.  
  31. public Animator m_animator = null; // holds the animator of the object (can be null)
  32. public Animation m_animation = null; // holds the animation of the object (can be null)
  33. public string m_animationIdleStr = "idle"; // the name of the idle animation
  34. public string m_animationWalkStr = "walk"; // the name of the walk animation
  35. public string m_animationRunStr = "run"; // the name of the run animation
  36.  
  37. Rigidbody rigidBody; //The variable we created to cache the GetComponent call.
  38. Collider collider;//variable to cache the collider getcomponent call
  39. Animator animator;//variable to catch the animator getcomponent call
  40.  
  41.  
  42. protected float m_lanePosXVariation = 0.0f;
  43. protected float m_lanePosZVariation = 0.0f;
  44. protected float m_speedVariation = 0.0f;
  45.  
  46. private bool ThresholdReached { get; set; }
  47.  
  48. public enum PathingStatus
  49. {
  50. RANDOM = 0
  51. }
  52.  
  53. void Awake()
  54. {
  55. m_speed = Random.Range(m_minSpeed, m_maxSpeed);
  56.  
  57. m_prevPedestrianNodes = new PedestrianNode[m_nodeVisitThreshold];
  58. for (int nIndex = 0; nIndex < m_prevPedestrianNodes.Length; nIndex++)
  59. m_prevPedestrianNodes[nIndex] = null;
  60.  
  61. if (PedestrianSystem.Instance)
  62. PedestrianSystem.Instance.RegisterObject(this);
  63.  
  64. rigidBody = GetComponent<Rigidbody>(); // This "caches" the GetComponent call. GetComponent "finds" the component you need, and finding takes a lot of time.
  65. collider = GetComponent<Collider>();
  66. animator = GetComponent<Animator>();
  67. }
  68.  
  69. IEnumerator Start()
  70. {
  71. if (PedestrianSystem.Instance)
  72. {
  73. m_speedVariation = Random.Range(0.0f, PedestrianSystem.Instance.m_globalSpeedVariation);
  74. m_lanePosXVariation = Random.Range(-PedestrianSystem.Instance.m_globalLanePosVariation, PedestrianSystem.Instance.m_globalLanePosVariation);
  75. m_lanePosZVariation = Random.Range(-PedestrianSystem.Instance.m_globalLanePosVariation, PedestrianSystem.Instance.m_globalLanePosVariation);
  76. }
  77.  
  78. DetermineSpeed(0.0f, true); // set idle (which is 0.0f if the object has an animator)
  79.  
  80. yield return new WaitForSeconds(Random.Range(m_startMovingDelayMin, m_startMovingDelayMax));
  81.  
  82. DetermineSpeed(Random.Range(m_minSpeed, m_maxSpeed));
  83.  
  84. yield return null;
  85. }
  86.  
  87. void Update()
  88. {
  89. if (!m_currentNode)
  90. {
  91. DetermineSpeed(0.0f, true); // idle animation as we are not walking anywhere
  92. return;
  93. }
  94.  
  95. Vector3 dir = m_currentNode.transform.position;
  96. dir.x += m_lanePosXVariation;
  97. dir.z += m_lanePosZVariation;
  98. dir = dir - (transform.position + m_offsetPosVal); // find the direction to the next node
  99.  
  100. Vector3 speed = dir.normalized * m_currentSpeed; // work out how fast we should travel in the desired directoin
  101.  
  102. if (ThresholdReached)
  103. {
  104. if (m_currentNode.m_waitAtNode)
  105. {
  106. if (m_speed != 0.0f)
  107. m_speedStoredWhileWaiting = m_speed;
  108.  
  109. DetermineSpeed(0.0f, true);
  110. rigidBody.velocity = Vector3.zero;
  111. }
  112. else
  113. {
  114. if (m_speedStoredWhileWaiting != 0.0f)
  115. {
  116. DetermineSpeed(m_speedStoredWhileWaiting);
  117. m_speedStoredWhileWaiting = 0.0f;
  118. }
  119.  
  120. m_prevPedestrianNodes[m_nodeVisitIndex] = m_currentNode;
  121. m_nodeVisitIndex++;
  122. if (m_nodeVisitIndex >= m_prevPedestrianNodes.Length)
  123. m_nodeVisitIndex = 0;
  124.  
  125. m_currentNode = m_currentNode.NextNode(this); // find another node or do something else
  126. ThresholdReached = false;
  127. }
  128. }
  129. else if (dir.magnitude > m_nodeThreshold)
  130. {
  131. if (rigidBody)) // if we have a rigidbody, use the following code to move us
  132. {
  133. rigidBody.velocity = speed; // set our rigidbody to this speed to move us by the determined speed
  134.  
  135. if (m_lookAtNode)
  136. transform.forward = Vector3.Slerp(transform.forward, dir.normalized, m_rotationSpeed * Time.deltaTime); // rotate our forward directoin over time to face the node we are moving towards
  137. }
  138. else // no rigidbody then use the following code to move us
  139. {
  140. if (collider) // it generally is a bad idea to move something with a collider, so we should tell someone about it if this is happening. See Unity Docs for more info: http://docs.unity3d.com/ScriptReference/Collider.html
  141. Debug.LogWarning("Pedestrian System Warning -> Object has a collider. You should think about moving the object with a rigidbody instead.");
  142.  
  143. transform.position += speed * Time.deltaTime; // move us by the determined speed
  144.  
  145. if (m_lookAtNode)
  146. transform.forward = Vector3.Slerp(transform.forward, dir.normalized, m_rotationSpeed * Time.deltaTime); // rotate our forward directoin over time to face the node we are moving towards
  147. }
  148.  
  149. if (m_onlyRotYAxis)
  150. transform.rotation = Quaternion.Euler(new Vector3(0.0f, transform.eulerAngles.y, 0.0f)); // only rotate around the Y axis.
  151. }
  152. else
  153. ThresholdReached = true;
  154. }
  155.  
  156. void FixedUpdate()
  157. {
  158. DetermineAnimation();
  159. }
  160.  
  161. void Destroy()
  162. {
  163. if (PedestrianSystem.Instance)
  164. PedestrianSystem.Instance.UnRegisterObject(this);
  165. }
  166.  
  167. public void Spawn(Vector3 a_pos, PedestrianNode a_startNode)
  168. {
  169. transform.position = a_pos - m_offsetPosVal;
  170. m_currentNode = a_startNode;
  171. }
  172.  
  173. public void DetermineSpeed(float a_speed, bool a_overrideCurrentSpeed = false)
  174. {
  175. m_speed = a_speed;
  176. m_currentSpeed = m_speed + m_speedVariation;
  177.  
  178. m_currentSpeed = m_currentSpeed * m_percentageOfSpeedToUse;
  179.  
  180. if (a_overrideCurrentSpeed)
  181. m_currentSpeed = m_speed;
  182. }
  183.  
  184. void DetermineAnimation()
  185. {
  186. if (animator)
  187. {
  188. if (m_currentSpeed <= 0.0f)
  189. animator.Play("idle");
  190. else
  191. animator.Play("walking");
  192.  
  193. animator.SetFloat("speed", m_currentSpeed / (m_maxSpeed + m_speedVariation));
  194. }
  195. else if (m_animation)
  196. {
  197. if (m_currentSpeed <= 0.0f)
  198. {
  199. if (!m_animation.IsPlaying(m_animationIdleStr))
  200. m_animation.Play(m_animationIdleStr);
  201. }
  202. else
  203. {
  204. if ((m_currentSpeed / (m_maxSpeed + m_speedVariation)) > 0.35f)
  205. {
  206. if (!m_animation.IsPlaying(m_animationRunStr))
  207. m_animation.Play(m_animationRunStr);
  208. }
  209. else
  210. {
  211. if (!m_animation.IsPlaying(m_animationWalkStr))
  212. m_animation.Play(m_animationWalkStr);
  213. }
  214. }
  215. }
  216. }
  217.  
  218. public bool HasVisitedNode(PedestrianNode a_node)
  219. {
  220. for (int nIndex = 0; nIndex < m_prevPedestrianNodes.Length; nIndex++)
  221. {
  222. if (m_prevPedestrianNodes[nIndex] == a_node)
  223. return true;
  224. }
  225.  
  226. return false;
  227. }
  228.  
  229. void OnDrawGizmos()
  230. {
  231. Gizmos.color = Color.yellow;
  232. Gizmos.DrawCube((transform.position + m_offsetPosVal), Vector3.one * 0.25f);
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement