Advertisement
Guest User

ScriptProjectile

a guest
Jun 19th, 2014
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  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.     //This time is to make sure it won't be running forever if it doesn't hit anything.
  14.     //If you want to limit the range of the projectile set this to a lower value.
  15.     public float selfDestructTime=3f;
  16.    
  17.     //It needs a DealDamage script, so we'll store one here.
  18.     private DealDamage DoDamage;
  19.     //We'll use this bool to check if we have been initiated by the player.
  20.     private bool Initiated=false;
  21.    
  22.     void Start() {
  23.         //Create a Deal Damage Component for us.
  24.         DoDamage = gameObject.AddComponent<DealDamage>();
  25.     }
  26.  
  27.     //This function will be called from the player to start our projectile.
  28.     public void Initialize() {
  29.         Initiated=true;
  30.         //This will start our countdown to self destruction
  31.         StartCoroutine(selfDestruct(selfDestructTime));
  32.     }
  33.    
  34.     void FixedUpdate () {
  35.         //If it has been initiated by the player
  36.         if(Initiated) {
  37.             //Tell the Rigid Body to move forward at the desired speed
  38.             rigidbody.MovePosition(rigidbody.transform.position+(transform.forward*ProjSpeed*Time.deltaTime));
  39.         }
  40.     }
  41.    
  42.     //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
  43.     void OnTriggerEnter(Collider other) {
  44.         //If it hasn't been initiated by the player, just stop here.
  45.         if(!Initiated) {
  46.             return;
  47.         }
  48.         //If the object colliding doesn't have the tag player and is not a trigger...
  49.         if(other.gameObject.tag!="Player" && !other.isTrigger) {
  50.             //If it's a rigid body, tell our DealDamage to attack it!
  51.             if (other.attachedRigidbody) {
  52.                 DoDamage.Attack(other.gameObject,ProjDamage,ProjHeight,ProjForce);
  53.             }
  54.             //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.
  55.             Destroy(this.gameObject);
  56.         }
  57.     }
  58.    
  59.     //Coroutine to wait for the set amount of seconds and destroy itself.
  60.     IEnumerator selfDestruct(float waitTime) {
  61.         yield return new WaitForSeconds(waitTime);
  62.         Destroy(this.gameObject);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement