Guest User

PlayerRanged

a guest
Jun 19th, 2014
916
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 PlayerRanged : MonoBehaviour {
  5.  
  6.     //We'll use those 2 to communicate with the rest of the kit.
  7.     private PlayerMove playerMove;
  8.     private CharacterMotor characterMotor;
  9.  
  10.     //This one will be our projectile object, it should have a ScriptProjectile in it.
  11.     //If you don't set one, I'll just create a sphere for you and use it, it'll be ugly! D:
  12.     public GameObject ProjectileObject;
  13.     //You can set a Transform here for the object to spawn from, if none is set I'll pick a
  14.     //Position in front of the player so no worries.
  15.     public Transform SpawnPosition;
  16.     //How much time should we wait before firing? We should wait for the animation probably.
  17.     public float WaitBeforeFiring =0.2f;
  18.     //How much cooldown should we have before firing again?
  19.     public float CooldownTime =1f;
  20.    
  21.     //I'll keep my last fired projectile on this variable for setting up purposes...
  22.     private GameObject LastFiredProjectile;
  23.    
  24.     private bool CanShoot;
  25.     // Use this for initialization
  26.     void Start () {
  27.         //We're supposed to be on the same gameobject as the PlayerMove...
  28.         //I'll need to talk with the animator so I'll just grab a reference from him.
  29.         playerMove = GetComponent<PlayerMove>();
  30.         //We're not on cooldown!
  31.         CanShoot=true;
  32.     }
  33.    
  34.     //Runs every frame
  35.     void Update () {
  36.         //If the player pressed the X key and he is not on cooldown...
  37.         if (Input.GetKeyDown(KeyCode.X)&&CanShoot) {
  38.             //First off, let's check if we're not already playing our ArmsThrow animation.
  39.             if(!CheckIfPlaying("ArmsThrow",1)) {
  40.                     //We're not playing it! So let's play it :D
  41.                     playerMove.animator.Play("ArmsThrow",1);
  42.                     //Now start a coroutine that will wait before firing so the animation plays a bit before spawning projectiles.
  43.                     StartCoroutine(WaitAndFire(WaitBeforeFiring));
  44.                     //Let's start our cooldown!
  45.                     CanShoot=false;
  46.                     StartCoroutine(CoolDown(CooldownTime));
  47.             }
  48.         }
  49.     }
  50.  
  51.     //This is an enumerator, it can stop and wait before resuming it's function.   
  52.     IEnumerator WaitAndFire(float waitTime)
  53.     {
  54.         //It'll wait for as much time as the float waitTime inputted.
  55.         yield return new WaitForSeconds(waitTime);
  56.         //First lets create a new vector 3 and zero it out.
  57.         Vector3 spawnPos = Vector3.zero;
  58.        
  59.         //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
  60.         if(!SpawnPosition) {
  61.             spawnPos = gameObject.transform.position + gameObject.transform.forward + Vector3.up*0.45f;
  62.         } else {
  63.             //If the player did set up a spawn position, just use it :D
  64.             spawnPos = SpawnPosition.position;
  65.         }
  66.        
  67.         //If the plaer was too lazy to create a Projectile Object let's make one for him and position it.
  68.         if(!ProjectileObject) {
  69.             LastFiredProjectile = CreateProjectile();
  70.             LastFiredProjectile.transform.rotation=gameObject.transform.rotation;
  71.             LastFiredProjectile.transform.position=spawnPos;
  72.         } else {
  73.             //If the player did create a Projectile Object lets instantiate it on our spawn position and rotate it propertly
  74.             LastFiredProjectile = GameObject.Instantiate(ProjectileObject,spawnPos,gameObject.transform.rotation) as GameObject;
  75.         }
  76.         //Lets initialize our newly spawned Projectile!
  77.         LastFiredProjectile.GetComponent<ScriptProjectile>().Initialize();
  78.     }
  79.    
  80.     //We'll use this as a cooldown so we can't spam the projectiles D:
  81.     IEnumerator CoolDown(float waitTime)
  82.     {
  83.         //It'll wait for as much time as the float waitTime inputted.
  84.         yield return new WaitForSeconds(waitTime);
  85.         //We can shoot now! The cooldown is over!
  86.         CanShoot=true;
  87.     }
  88.    
  89.     //This function will create a projectile for the lazy developer that didn't add one
  90.     //and add all the necessary stuff to it, then it'll return it to the funciton that called it.
  91.     GameObject CreateProjectile() {
  92.         //Creates a Sphere
  93.         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  94.         //Adds our Projectile script! Wouldn't be a projectile without it xD!
  95.         go.AddComponent<ScriptProjectile>();
  96.         //And a collider! It's important!
  97.         go.AddComponent<SphereCollider>();
  98.         //Also it needs a rigid body to move D: so lets add one
  99.         Rigidbody gobody = go.AddComponent<Rigidbody>();
  100.         //Now we'll make the rigid body into a Kinematic one
  101.         gobody.isKinematic=true;
  102.         //And let's deactivate gravity on it.
  103.         gobody.useGravity=false;
  104.         //Also, the collider we added... it should be a trigger :O
  105.         go.collider.isTrigger = true;
  106.                
  107.         //Now return the created game object
  108.         return go;
  109.     }
  110.    
  111.     bool CheckIfPlaying(string Anim,int Layer) {
  112.         //Grabs the AnimatorStateInfo out of our PlayerMove animator for the desired Layer.
  113.         AnimatorStateInfo AnimInfo = playerMove.animator.GetCurrentAnimatorStateInfo(Layer);
  114.         //Returns the bool we want, by checking if the string ANIM given is playing.
  115.         return AnimInfo.IsName(Anim);
  116.     }
  117. }
RAW Paste Data