Advertisement
vjanomolee

Missle Script

Mar 23rd, 2012
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Missle Script
  2. var heightLimit             =10.0;
  3. var explosion               :Transform;
  4. var sceneManager            :GameObject;
  5. var missileExplodeSound     :AudioClip;
  6. var missileSpeed                =5.0;
  7. var targetTag               :String;        //This is so you can change what tag is the target in the inspector instead of the script
  8. var missileTargets          :Transform[];          
  9.  
  10.  
  11. function Update () //Game Loop runs every frame
  12. {
  13.     var tar = GameObject.FindWithTag(targetTag);
  14.     if(tar)
  15.     {
  16.         var randomTarget = missileTargets[Random.Range(0, missileTargets.Length -1)];
  17.         transform.LookAt(randomTarget);
  18.         transform.Translate(Vector3.forward*missileSpeed*Time.deltaTime);
  19.            
  20.     }
  21.    
  22.     if(transform.position.y >= heightLimit) //check if the object is off screen
  23.     {
  24.         Destroy(gameObject);    //Remove the object from the scene
  25.     }
  26. }
  27.  
  28. function OnTriggerEnter (other : Collider) //Check for collisions with gameobjects tagged "Astroid"
  29. {
  30.     //Check for the asteroid
  31.     if(other.gameObject.tag == "Astroid")
  32.     {
  33.         //Reset the position of the enemy
  34.         other.transform.position.y = 8;
  35.         other.transform.position.x = Random.Range(-7.0,7.0);
  36.        
  37.         //Create the explosion on impact
  38.         if(explosion)
  39.         {
  40.             Instantiate(explosion, transform.position, transform.rotation); // Create prefab on collision
  41.             audio.PlayClipAtPoint(missileExplodeSound, transform.position);
  42.         }
  43.        
  44.         //Tell scene manager that we destroyed an enemy and add a point to the score
  45.         sceneManager.transform.GetComponent("ScriptSceneManager").AddScore();
  46.        
  47.         //Get rid of the bullet
  48.         Destroy(gameObject);                                        //Remove the object from the scene
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement