Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. //SeekSteer.js
  2.  
  3. var waypoints : Transform[];
  4. var waypointRadius : float = 1.5;
  5. var damping : float = 0.1;
  6. var loop : boolean = false;
  7. var speed : float = 2.0;
  8. var faceHeading : boolean = true;
  9.  
  10. private var targetHeading : Vector3;
  11. private var currentHeading : Vector3;
  12. private var targetwaypoint : int;
  13. private var xform : Transform;
  14. private var useRigidbody : boolean;
  15. private var rigidmember : Rigidbody;
  16.  
  17.  
  18. // Use this for initialization
  19. function Start() {
  20. xform = transform;
  21. currentHeading = xform.forward;
  22. if(waypoints.Length<=0)
  23. {
  24. Debug.Log("No waypoints on "+name);
  25. enabled = false;
  26. }
  27. targetwaypoint = 0;
  28. if(rigidbody!=null)
  29. {
  30. useRigidbody = true;
  31. rigidmember = rigidbody;
  32. }
  33. else
  34. {
  35. useRigidbody = false;
  36. }
  37. }
  38.  
  39.  
  40. // calculates a new heading
  41. function FixedUpdate() {
  42. targetHeading = waypoints[targetwaypoint].position - xform.position;
  43.  
  44. currentHeading = Vector3.Lerp(currentHeading,targetHeading,damping*Time.deltaTime);
  45. }
  46.  
  47. // moves us along current heading
  48. function Update(){
  49.  
  50. if(useRigidbody)
  51. rigidmember.velocity = currentHeading * speed;
  52. else
  53. xform.position +=currentHeading * Time.deltaTime * speed;
  54. if(faceHeading)
  55. xform.LookAt(xform.position+currentHeading);
  56.  
  57. if(Vector3.Distance(xform.position,waypoints[targetwaypoint].position)<=waypointRadius)
  58. {
  59. targetwaypoint++;
  60. if(targetwaypoint>=waypoints.Length)
  61. {
  62. targetwaypoint = 0;
  63. if(!loop)
  64. enabled = false;
  65. }
  66. }
  67. }
  68.  
  69.  
  70. // draws red line from waypoint to waypoint
  71. function OnDrawGizmos(){
  72.  
  73. Gizmos.color = Color.red;
  74. for(var i : int = 0; i< waypoints.Length;i++)
  75. {
  76. var pos : Vector3 = waypoints[i].position;
  77. if(i>0)
  78. {
  79. var prev : Vector3 = waypoints[i-1].position;
  80. Gizmos.DrawLine(prev,pos);
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement