Advertisement
Guest User

EnemyProjectileShooter

a guest
Jun 20th, 2014
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyProjectileShooter : MonoBehaviour {
  5.    
  6.     private CharacterMotor characterMotor;
  7.    
  8.     //This one will be our projectile object, it should have a ScriptProjectile in it.
  9.     //If you don't set one, I'll just create a sphere for you and use it, it'll be ugly! D:
  10.     public GameObject ProjectileObject;
  11.     //You can set a Transform here for the object to spawn from, if none is set I'll pick a
  12.     //Position in front of the player so no worries.
  13.     public Transform SpawnPosition;
  14.     //How much time should we wait before firing? We should wait for the animation probably.
  15.     public float WaitBeforeFiring =0.2f;
  16.     //How much cooldown should we have before firing again?
  17.     public float CooldownTime =1f;
  18.    
  19.     //I'll keep my last fired projectile on this variable for setting up purposes...
  20.     private GameObject LastFiredProjectile;
  21.    
  22.    
  23.     private bool CanShoot;
  24.     // Use this for initialization
  25.     void Start () {
  26.         //We're supposed to be on the same gameobject as the PlayerMove...
  27.         //I'll need to talk with the animator so I'll just grab a reference from him.
  28.         characterMotor = GetComponent<CharacterMotor>();
  29.         //We're not on cooldown!
  30.         CanShoot=true;
  31.     }
  32.    
  33.     // FixedUpdate isnt called as much as update
  34.     void FixedUpdate () {
  35.         //If the player pressed the X key and he is not on cooldown...
  36.         if (characterMotor.DistanceToTarget>0.03f&&CanShoot) {
  37.                     //Now start a coroutine that will wait before firing so the animation plays a bit before spawning projectiles.
  38.                     StartCoroutine(WaitAndFire(WaitBeforeFiring));
  39.                     CanShoot=false;
  40.                     StartCoroutine(CoolDown(CooldownTime));
  41.         }
  42.     }
  43.    
  44.         //This is an enumerator, it can stop and wait before resuming it's function.   
  45.     IEnumerator WaitAndFire(float waitTime)
  46.     {
  47.         //It'll wait for as much time as the float waitTime inputted.
  48.         yield return new WaitForSeconds(waitTime);
  49.         //First lets create a new vector 3 and zero it out.
  50.         Vector3 spawnPos = Vector3.zero;
  51.        
  52.         //If the player hasn't set a spawn position, let's make one for him in front of our character and kind of half his height
  53.         if(!SpawnPosition) {
  54.             spawnPos = gameObject.transform.position + gameObject.transform.forward*2 + Vector3.up*0.45f;
  55.         } else {
  56.             //If the player did set up a spawn position, just use it :D
  57.             spawnPos = SpawnPosition.position;
  58.         }
  59.        
  60.         //If the plaer was too lazy to create a Projectile Object let's make one for him and position it.
  61.         if(!ProjectileObject) {
  62.             LastFiredProjectile = CreateProjectile();
  63.             LastFiredProjectile.transform.rotation=gameObject.transform.rotation;
  64.             LastFiredProjectile.transform.position=spawnPos;
  65.         } else {
  66.             //If the player did create a Projectile Object lets instantiate it on our spawn position and rotate it propertly
  67.             LastFiredProjectile = GameObject.Instantiate(ProjectileObject,spawnPos,gameObject.transform.rotation) as GameObject;
  68.         }
  69.         //Lets initialize our newly spawned Projectile!
  70.         LastFiredProjectile.GetComponent<ScriptProjectile>().Owner=this.gameObject;
  71.         LastFiredProjectile.GetComponent<ScriptProjectile>().Initialize();
  72.     }
  73.    
  74.     //We'll use this as a cooldown so we can't spam the projectiles D:
  75.     IEnumerator CoolDown(float waitTime)
  76.     {
  77.         //It'll wait for as much time as the float waitTime inputted.
  78.         yield return new WaitForSeconds(waitTime);
  79.         //We can shoot now! The cooldown is over!
  80.         CanShoot=true;
  81.     }
  82.    
  83.     //This function will create a projectile for the lazy developer that didn't add one
  84.     //and add all the necessary stuff to it, then it'll return it to the funciton that called it.
  85.     GameObject CreateProjectile() {
  86.         //Creates a Sphere
  87.         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  88.         //Adds our Projectile script! Wouldn't be a projectile without it xD!
  89.         go.AddComponent<ScriptProjectile>();
  90.         //And a collider! It's important!
  91.         go.AddComponent<SphereCollider>();
  92.         //Also it needs a rigid body to move D: so lets add one
  93.         Rigidbody gobody = go.AddComponent<Rigidbody>();
  94.         //Now we'll make the rigid body into a Kinematic one
  95.         gobody.isKinematic=true;
  96.         //And let's deactivate gravity on it.
  97.         gobody.useGravity=false;
  98.         //Also, the collider we added... it should be a trigger :O
  99.         go.collider.isTrigger = true;
  100.                
  101.         //Now return the created game object
  102.         return go;
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement