Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BasicShooter : MonoBehaviour {
  5. public Path path;
  6. public float speed = 20.0f;
  7. public float mass = 5.0f;
  8. public bool isLooping = true;
  9.  
  10. //Actual speed of the vehicle
  11. private float curSpeed;
  12.  
  13. private int curPathIndex;
  14. private float pathLength;
  15. private Vector3 targetPoint;
  16. private GameObject returnPath;
  17.  
  18. Vector3 velocity;
  19.  
  20. // Use this for initialization
  21. void Start ()
  22. {
  23. float pathVal = Mathf.Floor(Random.value * 3);
  24. GameObject path0 = GameObject.Find ("PeonWaypoints0");
  25. GameObject path1 = GameObject.Find ("PeonWaypoints1");
  26. GameObject path2 = GameObject.Find ("PeonWaypoints2");
  27. returnPath = GameObject.Find ("ReturnWaypoints");
  28. if (pathVal == 0)
  29. path = (Path)path0.GetComponent (typeof(Path));
  30. else if (pathVal == 1)
  31. path = (Path)path1.GetComponent (typeof(Path));
  32. else if (pathVal == 2)
  33. path = (Path)path2.GetComponent (typeof(Path));
  34. else
  35. Debug.Log ("Randomizer Error");
  36. path.isDebug = true;
  37. pathLength = path.Length;
  38. curPathIndex = 0;
  39.  
  40. //get the current velocity of the vehicle
  41. velocity = transform.forward;
  42. }
  43.  
  44. // Update is called once per frame
  45. void Update ()
  46. {
  47. //Unify the speed
  48. curSpeed = speed * Time.deltaTime;
  49.  
  50. targetPoint = path.GetPoint(curPathIndex);
  51.  
  52. //If reach the radius within the path then move to next point in the path
  53. if(Vector3.Distance(transform.position, targetPoint) < path.Radius)
  54. {
  55. //Don't move the vehicle if path is finished
  56. if (curPathIndex < pathLength - 1)
  57. curPathIndex ++;
  58. else if (isLooping)
  59. curPathIndex = 0;
  60. else
  61. return;
  62. }
  63.  
  64. //Calculate the next Velocity towards the path
  65. if(curPathIndex >= pathLength - 1 && !isLooping)
  66. velocity += Steer(targetPoint, true);
  67. else
  68. velocity += Steer(targetPoint);
  69.  
  70. transform.position += velocity; //Move the vehicle according to the velocity
  71. transform.rotation = Quaternion.LookRotation(velocity); //Rotate the vehicle towards the desired Velocity
  72. }
  73.  
  74. //Steering algorithm to steer the vector towards the target
  75. public Vector3 Steer(Vector3 target, bool bFinalPoint = false)
  76. {
  77. //Calculate the directional vector from the current position towards the target point
  78. Vector3 desiredVelocity = (target - transform.position);
  79. float dist = desiredVelocity.magnitude;
  80. Debug.Log (desiredVelocity);
  81.  
  82. //Normalise the desired Velocity
  83. desiredVelocity.Normalize();
  84.  
  85. //Calculate the velocity according to the speed
  86. if (bFinalPoint && dist < 10.0f)
  87. {
  88. desiredVelocity *= (curSpeed * (dist / 10.0f));
  89. if (dist <= 1);
  90. {
  91. path = (Path)returnPath.GetComponent (typeof(Path)); <-------------------------
  92. curPathIndex = 0;
  93. }
  94. }
  95. else
  96. desiredVelocity *= curSpeed;
  97.  
  98. //Calculate the force Vector
  99. Vector3 steeringForce = desiredVelocity - velocity;
  100. Vector3 acceleration = steeringForce / mass;
  101.  
  102. return acceleration;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement