Advertisement
Guest User

Tower defense w unity

a guest
Apr 18th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //SKRYPT NA OGÓLNY RUCH GŁOWICY(TURETU) CZAS I KILKA INNYCH RZECZY KTÓRE MNIE DENERWUJĄ
  2. #pragma strict
  3. //zmienne globalne
  4. var myProjectile : GameObject;
  5. var reloadTime : float = 1f;// prędkość reoladu
  6. var turnSpeed : float = 5f; //prędkość obracania się wieży
  7. var firePauseTime : float = 0;
  8. var muzzleEffect : GameObject;
  9. var errorAmount : float = .001f;
  10. var myTarget : Transform;
  11. var muzzlePosition : Transform [];//[]para elementów dobre info
  12. var turretBall : Transform;
  13.  
  14. private var nextFireTime : float;//przerwa pomiędzy strzałem
  15. private var nextMoveTime : float;
  16. private var desiredRotation : Quaternion;
  17. private var aimError : float;
  18.  
  19.  
  20. function Start ()
  21. {
  22.  
  23. }
  24.  
  25. function Update ()
  26. {
  27.  
  28.     if(myTarget)
  29.     {
  30.         if (Time.time >= nextMoveTime)
  31.         {
  32.             CalculateAimPosition(myTarget.position);
  33.             turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
  34.         }
  35.         if(Time.time >= nextFireTime)
  36.         {
  37.             FireProjectile();
  38.         }
  39.     }
  40.    
  41. }
  42. function OnTriggerEnter (other : Collider)
  43. {
  44.     nextFireTime = Time.time+(reloadTime*.5);//czas przeładowania to 0.5s
  45.     myTarget = other.gameObject.transform;
  46. }
  47.  
  48. function OnTriggerExit(other : Collider)
  49. {
  50.     if(other.gameObject.transform == myTarget)
  51.     {
  52.     myTarget = null;
  53.     }
  54. }
  55. function CalculateAimPosition(targetPos : Vector3)
  56. {
  57.      var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
  58.      desiredRotation = Quaternion.LookRotation(aimPoint);
  59. }
  60.     function CalculateAimError()
  61. {
  62.     aimError = Random.Range(-errorAmount, errorAmount);
  63. }
  64.  
  65. function FireProjectile()
  66. {
  67.     //audio.Play();
  68.     nextFireTime = Time.time+reloadTime;
  69.     nextMoveTime = Time.time+firePauseTime;
  70.     CalculateAimError();
  71.  
  72. for(theMuzzlePos in muzzlePositions)
  73. {
  74.     Istantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
  75.     Istantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement