Guest User

ScriptProjectile

a guest
Jun 20th, 2014
745
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ScriptProjectile : MonoBehaviour {
  5.  
  6.     //Information about our projectile... how much damage? How much should it push
  7.     //the victim on height? how much force should add on the push? how fast should it go?
  8.     public int ProjDamage = 1;
  9.     public float ProjHeight = 2f;
  10.     public float ProjForce =10f;
  11.     public float ProjSpeed=15f;
  12.    
  13.     //Let's store our owner so we don't hit him.
  14.     public GameObject Owner;
  15.    
  16.     //This time is to make sure it won't be running forever if it doesn't hit anything.
  17.     //If you want to limit the range of the projectile set this to a lower value.
  18.     public float selfDestructTime=3f;
  19.    
  20.     //It needs a DealDamage script, so we'll store one here.
  21.     private DealDamage DoDamage;
  22.     //We'll use this bool to check if we have been initiated by the player.
  23.     private bool Initiated=false;
  24.    
  25.     void Start() {
  26.         //Create a Deal Damage Component for us.
  27.         DoDamage = gameObject.AddComponent<DealDamage>();
  28.     }
  29.  
  30.     //This function will be called from the player to start our projectile.
  31.     public void Initialize() {
  32.         Initiated=true;
  33.         //This will start our countdown to self destruction
  34.         StartCoroutine(selfDestruct(selfDestructTime));
  35.     }
  36.    
  37.     void FixedUpdate () {
  38.         //If it has been initiated by the player
  39.         if(Initiated) {
  40.             //Tell the Rigid Body to move forward at the desired speed
  41.             rigidbody.MovePosition(rigidbody.transform.position+(transform.forward*ProjSpeed*Time.deltaTime));
  42.         }
  43.     }
  44.    
  45.     //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
  46.     void OnTriggerEnter(Collider other) {
  47.                 //If it hasn't been initiated by the player, just stop here.
  48.                 if(!Initiated) {
  49.                         return;
  50.                 }
  51.                 //Was an Owner set for us? It's probably an enemy, let's check, if it is we'll let him hit the player
  52.                 if(Owner) {
  53.                     //Am I the owner? If I am I don't want to hit myself so return
  54.                     if(Owner==other.gameObject) {
  55.                         return;
  56.                     }
  57.                     //I'm not the owner, but the owner is an enemy, so if the collider is a player we should...
  58.                     if(Owner.tag=="Enemy" && other.gameObject.tag=="Player" && !other.isTrigger) {
  59.                         if (other.attachedRigidbody) {
  60.                                 //DAMAGE THE HELL OUT OF HIM!
  61.                                 DoDamage.Attack(other.gameObject,ProjDamage,ProjHeight,ProjForce);
  62.                                 Destroy(this.gameObject);
  63.                                 return;
  64.                         }
  65.                     }
  66.                    
  67.                 }
  68.                
  69.                 //If the object colliding doesn't have the tag player and is not a trigger...
  70.                 if(other.gameObject.tag!="Player" && !other.isTrigger) {
  71.                         //If it's a rigid body, tell our DealDamage to attack it!
  72.                     if (other.attachedRigidbody) {
  73.                             DoDamage.Attack(other.gameObject,ProjDamage,ProjHeight,ProjForce);
  74.                     }
  75.                 //If it isn't we still probably want to destroy our projectile since it has a collider, so destroy it wether it is a rigid body or not.
  76.                 Destroy(this.gameObject);
  77.         }
  78.     }
  79.    
  80.     //Coroutine to wait for the set amount of seconds and destroy itself.
  81.     IEnumerator selfDestruct(float waitTime) {
  82.         yield return new WaitForSeconds(waitTime);
  83.         Destroy(this.gameObject);
  84.     }
  85. }
RAW Paste Data