Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. var turretHead : Transform;
  2. var turretBarrel : Transform;
  3. var target : Transform;
  4. var rotationToLook : Quaternion;
  5. var rotationEuler : Vector3;
  6.  
  7. var maxVerticalLook : float = 45;
  8. var maxHorizontalLook : float = 45;
  9. var maxDistance : float = 30;
  10.  
  11. var distance : float;
  12.  
  13. var targetLocked : boolean = false;
  14. var targetAquiring : boolean = false;
  15. var targetFiring : boolean = false;
  16.  
  17. var rocket : Rigidbody; //our prefab
  18. var throwPower : float = 500; //power variable determines how fast this object will be "shot out"
  19.  
  20. var lookAt : Transform;
  21.  
  22. function Update () {
  23.  
  24. // Calculate relative position to calculate rotation and distance.
  25. var relativePos = target.position - transform.position;
  26.  
  27. // Calculate Distance of Target
  28. distance = Vector3.Distance(transform.position,target.position);
  29.  
  30. // Calculate Rotation
  31. rotationToLook = Quaternion.LookRotation(relativePos);
  32. rotationEuler = rotationToLook.eulerAngles; // Used for debugging
  33.  
  34. // Check if target is in range (not yet done) and also if in targeting area
  35. if ( distance < maxDistance && checkYBounds( rotationToLook, maxHorizontalLook ) && checkXBounds( rotationToLook, maxVerticalLook ) ) {
  36.  
  37. // Debug.Log("Turret is in Range");
  38.  
  39. if ( targetAquiring == false ) {
  40.  
  41. // Starting to Aquire
  42. StartCoroutine("acquiringTarget");
  43.  
  44.  
  45. } if ( targetLocked == true && targetFiring == false ) {
  46.  
  47. // Fire Bullet
  48. StartCoroutine("fireBullet");
  49.  
  50.  
  51. }
  52.  
  53.  
  54. } else {
  55.  
  56. // Revert to Home Position
  57. rotationEuler = Vector3 ( 0, 0, 0 );
  58.  
  59. }
  60.  
  61.  
  62. // Move Turret Head
  63. var turretHeadEuler : Vector3;
  64. turretHeadEuler.y = rotationEuler.y;
  65.  
  66. var turretHeadqr : Quaternion = Quaternion.Euler(turretHeadEuler);
  67.  
  68. turretHead.localRotation = Quaternion.Lerp(turretHead.localRotation, turretHeadqr, Time.deltaTime * 5);
  69.  
  70.  
  71.  
  72. var turretBarrelEuler : Vector3;
  73. turretBarrelEuler.z = -rotationEuler.x;
  74. turretBarrelEuler.y = 0;
  75.  
  76.  
  77. var turretBarrelqr : Quaternion = Quaternion.Euler(turretBarrelEuler);
  78.  
  79. turretBarrel.localRotation = Quaternion.Lerp(turretBarrel.localRotation, turretBarrelqr, Time.deltaTime * 5);
  80.  
  81.  
  82.  
  83.  
  84. }
  85.  
  86. function checkYBounds( rotationToLook : Quaternion, maxLook : float ) {
  87.  
  88. if ( rotationToLook.eulerAngles.y < maxLook || rotationToLook.eulerAngles.y > ( 360 - maxLook ) ) {
  89.  
  90. return true;
  91.  
  92. } else {
  93.  
  94. return false;
  95.  
  96. }
  97. }
  98.  
  99. function checkXBounds( rotationToLook : Quaternion, maxLook : float ) {
  100.  
  101. if ( rotationToLook.eulerAngles.x < maxLook || rotationToLook.eulerAngles.x > ( 360 - maxLook ) ) {
  102.  
  103. return true;
  104.  
  105. } else {
  106.  
  107. return false;
  108.  
  109. }
  110. }
  111.  
  112.  
  113.  
  114. function acquiringTarget() {
  115.  
  116. targetAquiring = true;
  117.  
  118. yield WaitForSeconds(3);
  119.  
  120. targetLocked = true;
  121.  
  122. Debug.Log("Auto Turret - Target Acquired - Ready to fire");
  123.  
  124.  
  125. }
  126.  
  127.  
  128. function fireBullet() {
  129.  
  130. targetFiring = true;
  131.  
  132. var clone : Rigidbody; //variable within a variable
  133.  
  134. clone = Instantiate(rocket, rocket.transform.position, rocket.transform.rotation); //the clone variable holds our instantiate action
  135.  
  136. clone.gameObject.SetActive( true );
  137.  
  138.  
  139. clone.rigidbody.AddForce(clone.transform.forward * throwPower);
  140.  
  141. yield WaitForSeconds(1);
  142.  
  143. targetFiring = false;
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement