Guest User

PlayerPunchPhysicsKitWithoutAnims

a guest
Jun 19th, 2014
822
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, see if the punch animations are playing.
  58.             if(!CheckIfPlaying("ArmsThrow",1)&&!CheckIfPlaying("ArmsThrow",1)) {
  59.                 //If I got here no punch animations are playing so we can punch now :D
  60.                 if(punchleft) {
  61.                     //Tell the anim ator to play the left punch, and change the bool so next punch will be right punch!
  62.                     playerMove.animator.Play("ArmsThrow",1);
  63.                     punchleft=false;
  64.                     //Then start the coroutine so the punch effect happens when the animation already reached the interest part.
  65.                     StartCoroutine(WaitAndPunch());
  66.                 } else {
  67.                     playerMove.animator.Play("ArmsThrow",1);
  68.                     punchleft=true;
  69.                     StartCoroutine(WaitAndPunch());
  70.                 }
  71.             }
  72.         }
  73.     }
  74.    
  75.     IEnumerator WaitAndPunch()
  76.     {
  77.         //Wait for 0.12f time and then punch them in their stupid faces!
  78.         yield return StartCoroutine(Wait(0.12f));
  79.         PunchThem();
  80.     }
  81.    
  82.     IEnumerator WaitAndStopPunch() {
  83.         //Wait for 0.1f time before stopping the punch
  84.         yield return StartCoroutine(Wait(0.1f));
  85.         StopPunch();
  86.     }
  87.    
  88.     //Coroutine for cool waiting stuff
  89.     IEnumerator Wait(float duration)
  90.     {
  91.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  92.             yield return 0;
  93.     }
  94.    
  95.     void PunchThem() {
  96.         //Enable our cool Punching Hitbox to check for enemies in there.
  97.         PunchHitBox.enabled=true;
  98.         //Turn our Punchin bool to true so our TriggerStay will check for people being punched.
  99.         Punching=true;
  100.         //Start the coroutine that will wait for a moment and stop the punching stuff turning bools back to false.
  101.         StartCoroutine(WaitAndStopPunch());
  102.     }
  103.    
  104.     void StopPunch() {
  105.         //Turn stuff back to false so it'll stop checking for people on hitbox
  106.         PunchHitBox.enabled=false;
  107.         Punching=false;
  108.         //Clear the List of people that got punched on this punch.
  109.         BeingPunched.Clear();
  110.     }
  111.    
  112.     //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
  113.     void OnTriggerStay(Collider other) {
  114.         //If we're not punching, forget about it, just stop right here!
  115.         if(!Punching) {
  116.             return;
  117.         }
  118.         //If we are punching, and the tag on our trigger zone has a RigidBody and it's not tagged Player then...
  119.         if (other.attachedRigidbody&&other.gameObject.tag!="Player") {
  120.             //If this guy on our trigger zone is not on our List of people already punched with this punch
  121.             if(!BeingPunched.Contains(other.gameObject)) {
  122.                 //Call the DealDamage script telling it to punch the hell out of this guy
  123.                 DoDamage.Attack(other.gameObject,PunchDamage,PushHeight,PushForce);
  124.                 //Add him to the list, so we won't hit him again with the same punch.
  125.                 BeingPunched.Add(other.gameObject);
  126.             }
  127.         }
  128.     }
  129.    
  130.     //This will return a TRUE/FALSE on the animation we want to check if is playing.
  131.     bool CheckIfPlaying(string Anim,int Layer) {
  132.         //Grabs the AnimatorStateInfo out of our PlayerMove animator for the desired Layer.
  133.         AnimatorStateInfo AnimInfo = playerMove.animator.GetCurrentAnimatorStateInfo(Layer);
  134.         //Returns the bool we want, by checking if the string ANIM given is playing.
  135.         return AnimInfo.IsName(Anim);
  136.     }
  137. }
RAW Paste Data