Guest User

PlayerPunchPhysicsKitWithAnims

a guest
Jun 19th, 2014
1,347
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.    
  5. public class PlayerMelee : MonoBehaviour {
  6.  
  7.     //We'll use those 3 to communicate with the rest of the kit.
  8.     private PlayerMove playerMove;
  9.     private CharacterMotor characterMotor;
  10.     private DealDamage DoDamage;
  11.    
  12.     //This will be used to cycle through 2 different punch animations
  13.     private bool punchleft;
  14.     //This will be used to check if we should ge giving damage.
  15.     private bool Punching;
  16.  
  17.     private List<GameObject> BeingPunched = new List<GameObject>();
  18.    
  19.     //These are our public vars, PunchHitBox should be like the GrabBox,
  20.     //and should encompass the area you want your punch to cover
  21.     public BoxCollider PunchHitBox;
  22.    
  23.     public int PunchDamage = 1;
  24.     public float PushHeight = 4;
  25.     public float PushForce =10;
  26.    
  27.     // Use this for initialization
  28.     void Start () {
  29.         //We're supposed to be on the same gameobject as the PlayerMove,
  30.         //CharacterMotor etc, so lets get them as reference!
  31.         playerMove = GetComponent<PlayerMove>();
  32.         characterMotor = GetComponent<CharacterMotor>();
  33.         DoDamage = GetComponent<DealDamage>();
  34.        
  35.         //Did you even make a PunchBox? Or you were lazy and didn't make one?
  36.         if(!PunchHitBox)
  37.         {
  38.             GameObject GameObjectPunchHitBox = new GameObject();
  39.             PunchHitBox = GameObjectPunchHitBox.AddComponent<BoxCollider>();
  40.             GameObjectPunchHitBox.collider.isTrigger = true;
  41.             GameObjectPunchHitBox.transform.parent = transform;
  42.             //Let's place it a little but farther away from us.
  43.             GameObjectPunchHitBox.transform.localPosition = new Vector3(0f, 0f, 1f);
  44.             //It should Ignore Raycast so let's put it on layer 2.
  45.             GameObjectPunchHitBox.layer = 2;
  46.             Debug.LogWarning("You were too lazy to make a PunchHitBox so I made one for you, happy?", GameObjectPunchHitBox);
  47.         }
  48.        
  49.         //Also lets turn off our PunchHitBox, we'll only turn that on while punching
  50.         //so the Grabbing script doesn't get confused with it.
  51.         PunchHitBox.enabled=false;
  52.     }
  53.    
  54.     // Update is called once per frame
  55.     void Update () {
  56.         if (Input.GetKeyDown(KeyCode.Z)) {
  57.             //First off, let's check if we're not already punching,
  58.             //see if the punch animations are playing on Layer 1 (The one with the arms).
  59.             if(!CheckIfPlaying("RightPunch",1)&&!CheckIfPlaying("LeftPunch",1)) {
  60.                 //If I got here no punch animations are playing so we can punch now :D
  61.                 if(punchleft) {
  62.                     //Tell the anim ator to play the left punch, and change the bool so next punch will be right punch!
  63.                     playerMove.animator.Play("LeftPunch",1);
  64.                     punchleft=false;
  65.                     //Then start the coroutine so the punch effect happens when the animation already reached the interest part.
  66.                     StartCoroutine(WaitAndPunch());
  67.                 } else {
  68.                     playerMove.animator.Play("RightPunch",1);
  69.                     punchleft=true;
  70.                     StartCoroutine(WaitAndPunch());
  71.                 }
  72.             }
  73.         }
  74.     }
  75.    
  76.     IEnumerator WaitAndPunch()
  77.     {
  78.         //Wait for 0.12f time and then punch them in their stupid faces!
  79.         yield return StartCoroutine(Wait(0.12f));
  80.         PunchThem();
  81.     }
  82.    
  83.     IEnumerator WaitAndStopPunch() {
  84.         //Wait for 0.1f time before stopping the punch
  85.         yield return StartCoroutine(Wait(0.1f));
  86.         StopPunch();
  87.     }
  88.    
  89.     //Coroutine for cool waiting stuff
  90.     IEnumerator Wait(float duration)
  91.     {
  92.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  93.             yield return 0;
  94.     }
  95.    
  96.     void PunchThem() {
  97.         //Enable our cool Punching Hitbox to check for enemies in there.
  98.         PunchHitBox.enabled=true;
  99.         //Turn our Punchin bool to true so our TriggerStay will check for people being punched.
  100.         Punching=true;
  101.         //Start the coroutine that will wait for a moment and stop the punching stuff turning bools back to false.
  102.         StartCoroutine(WaitAndStopPunch());
  103.     }
  104.    
  105.     void StopPunch() {
  106.         //Turn stuff back to false so it'll stop checking for people on hitbox
  107.         PunchHitBox.enabled=false;
  108.         Punching=false;
  109.         //Clear the List of people that got punched on this punch.
  110.         BeingPunched.Clear();
  111.     }
  112.    
  113.     //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
  114.     void OnTriggerStay(Collider other) {
  115.         //If we're not punching, forget about it, just stop right here!
  116.         if(!Punching) {
  117.             return;
  118.         }
  119.         //If we are punching, and the tag on our trigger zone has a RigidBody and it's not tagged Player then...
  120.         if (other.attachedRigidbody&&other.gameObject.tag!="Player") {
  121.             //If this guy on our trigger zone is not on our List of people already punched with this punch
  122.             if(!BeingPunched.Contains(other.gameObject)) {
  123.                 //Call the DealDamage script telling it to punch the hell out of this guy
  124.                 DoDamage.Attack(other.gameObject,PunchDamage,PushHeight,PushForce);
  125.                 //Add him to the list, so we won't hit him again with the same punch.
  126.                 BeingPunched.Add(other.gameObject);
  127.             }
  128.         }
  129.     }
  130.    
  131.     //This will return a TRUE/FALSE on the animation we want to check if is playing.
  132.     bool CheckIfPlaying(string Anim,int Layer) {
  133.         //Grabs the AnimatorStateInfo out of our PlayerMove animator for the desired Layer.
  134.         AnimatorStateInfo AnimInfo = playerMove.animator.GetCurrentAnimatorStateInfo(Layer);
  135.         //Returns the bool we want, by checking if the string ANIM given is playing.
  136.         return AnimInfo.IsName(Anim);
  137.     }
  138. }
RAW Paste Data