Advertisement
Guest User

PunchWithSlowdown

a guest
Aug 5th, 2014
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using PigeonCoopToolkit.Effects.Trails;
  5.  
  6. public class PlayerMelee : MonoBehaviour {
  7.  
  8.     //We'll use those 3 to communicate with the rest of the kit.
  9.     private PlayerMove playerMove;
  10.     private CharacterMotor characterMotor;
  11.     private DealDamage DoDamage;
  12.    
  13.     //This will be used to cycle through 2 different punch animations
  14.     private bool punchleft;
  15.     //This will be used to check if we should ge giving damage.
  16.     private bool Punching;
  17.  
  18.     private List<GameObject> BeingPunched = new List<GameObject>();
  19.    
  20.     //These are our public vars, PunchHitBox should be like the GrabBox,
  21.     //and should encompass the area you want your punch to cover
  22.     public BoxCollider PunchHitBox;
  23.    
  24.     public int PunchDamage = 1;
  25.     public float PushHeight = 4;
  26.     public float PushForce =10;
  27.    
  28.    
  29.      //the factor used to slow down time  
  30.     public float slowFactor = 4f;
  31.     //the new time scale  
  32.     private float newTimeScale;
  33.    
  34.      //the factor used to slow down time  
  35.     private int FramesToGetBack = 0;
  36.     public int WaitFramesSlowMotion=4;
  37.    
  38.     public float PunchDrag=10f;
  39.     public Trail LeftHandTrail;
  40.     public Trail RightHandTrail;
  41.     public float regularpunchshake;
  42.     public float regularpunchshakeamount;
  43.     public float regularpunchdecfactor;
  44.     public TriggeredSpawner PunchFX;
  45.     // Use this for initialization
  46.     void Start () {
  47.         //We're supposed to be on the same gameobject as the PlayerMove,
  48.         //CharacterMotor etc, so lets get them as reference!
  49.         playerMove = GetComponent<PlayerMove>();
  50.         characterMotor = GetComponent<CharacterMotor>();
  51.         DoDamage = GetComponent<DealDamage>();
  52.        
  53.         //Did you even make a PunchBox? Or you were lazy and didn't make one?
  54.         if(!PunchHitBox)
  55.         {
  56.             GameObject GameObjectPunchHitBox = new GameObject();
  57.             PunchHitBox = GameObjectPunchHitBox.AddComponent<BoxCollider>();
  58.             GameObjectPunchHitBox.collider.isTrigger = true;
  59.             GameObjectPunchHitBox.transform.parent = transform;
  60.             //Let's place it a little but farther away from us.
  61.             GameObjectPunchHitBox.transform.localPosition = new Vector3(0f, 0f, 1f);
  62.             //It should Ignore Raycast so let's put it on layer 2.
  63.             GameObjectPunchHitBox.layer = 2;
  64.             Debug.LogWarning("You were too lazy to make a PunchHitBox so I made one for you, happy?", GameObjectPunchHitBox);
  65.         }
  66.        
  67.         //Also lets turn off our PunchHitBox, we'll only turn that on while punching
  68.         //so the Grabbing script doesn't get confused with it.
  69.         PunchHitBox.enabled=false;
  70.     }
  71.    
  72.     // Update is called once per frame
  73.     void Update () {
  74.         //If frames to get back is bigger than 0
  75.         if(FramesToGetBack>0) {
  76.             //Then decrease frames to get back by 1
  77.             FramesToGetBack--;
  78.         } else {
  79.             //If frames to get back IS 0 and our timescale is different than 1.
  80.             if (Time.timeScale != 1.0f)  
  81.             {  
  82.                 //Then let's stop our slowmotion.
  83.                 StopSlowMotion();
  84.             }
  85.         }
  86.         if (Input.GetButtonDown ("Punch")) {
  87.             //First off, let's check if we're not already punching, see if the punch animations are playing.
  88.             if(!CheckIfPlaying("RightPunch",2)&&!CheckIfPlaying("LeftPunch",2)) {
  89.                 //If I got here no punch animations are playing so we can punch now :D
  90.                 if(punchleft) {
  91.                     //Tell the anim ator to play the left punch, and change the bool so next punch will be right punch!
  92.                     playerMove.animator.Play("LeftPunch",2);
  93.                     LeftHandTrail.Emit = true;
  94.                     punchleft=false;
  95.                     //Then start the coroutine so the punch effect happens when the animation already reached the interest part.
  96.                     gameObject.rigidbody.drag=PunchDrag;
  97.                     StartCoroutine(WaitAndPunch());
  98.                 } else {
  99.                     playerMove.animator.Play("RightPunch",2);
  100.                     RightHandTrail.Emit = true;
  101.                     punchleft=true;
  102.                     gameObject.rigidbody.drag=PunchDrag;
  103.                     StartCoroutine(WaitAndPunch());
  104.                 }
  105.             }
  106.         }
  107.     }
  108.    
  109.     //This function stops slowmotion
  110.     void StopSlowMotion() {
  111.         //Set our timescale to 1
  112.         Time.timeScale = 1.0f;  
  113.         //multiply our fixeddelta time by our slowFactor
  114.         Time.fixedDeltaTime = Time.fixedDeltaTime*slowFactor;  
  115.         //Multiply our maximum delta time by our slowFactor
  116.         Time.maximumDeltaTime = Time.maximumDeltaTime*slowFactor;
  117.     }
  118.    
  119.     //This funciton will start our slow motion
  120.     void SlowMotion() {
  121.         //if the game is running normally (Timescale 1)
  122.             if (Time.timeScale == 1.0f)  
  123.             {  
  124.                 //assign the 'newTimeScale' to the current 'timeScale'
  125.                 Time.timeScale = newTimeScale;  
  126.                 //proportionally reduce the 'fixedDeltaTime', so that the Rigidbody simulation can react correctly
  127.                 //dividing the fixedDeltatime by slowFactor
  128.                 Time.fixedDeltaTime = Time.fixedDeltaTime/slowFactor;  
  129.                 //The maximum amount of time of a single frame, figured out by dividing MaximumDeltaTime by the slowFactor
  130.                 Time.maximumDeltaTime = Time.maximumDeltaTime/slowFactor;
  131.                 //Set our FramesToGetBack to be our WaitFramesSlowMotion. This will be decreased by 1 every frame until 0, then it'll get back to normal.
  132.                 FramesToGetBack=WaitFramesSlowMotion;
  133.             }  
  134.     }
  135.    
  136.     IEnumerator WaitAndPunch()
  137.     {
  138.         //Wait for 0.12f time and then punch them in their stupid faces!
  139.         yield return StartCoroutine(Wait(0.12f));
  140.         PunchThem();
  141.     }
  142.    
  143.     IEnumerator WaitAndStopPunch() {
  144.         //Wait for 0.1f time before stopping the punch
  145.         yield return StartCoroutine(Wait(0.1f));
  146.         StopPunch();
  147.     }
  148.    
  149.     //Coroutine for cool waiting stuff
  150.     IEnumerator Wait(float duration)
  151.     {
  152.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
  153.             yield return 0;
  154.     }
  155.    
  156.     void PunchThem() {
  157.         PunchFX.ActivateCodeTriggeredEvent1();
  158.         //Debug.Log("PUNCH!");
  159.         //Enable our cool Punching Hitbox to check for enemies in there.
  160.         PunchHitBox.enabled=true;
  161.         //Turn our Punchin bool to true so our TriggerStay will check for people being punched.
  162.         Punching=true;
  163.        
  164.         Camera.main.GetComponent<CameraShaker>().DoTheShake(regularpunchshake,regularpunchshakeamount,regularpunchdecfactor);
  165.         //Start the coroutine that will wait for a moment and stop the punching stuff turning bools back to false.
  166.         StartCoroutine(WaitAndStopPunch());
  167.     }
  168.    
  169.     void StopPunch() {
  170.         LeftHandTrail.Emit = false;
  171.         RightHandTrail.Emit = false;
  172.         //Turn stuff back to false so it'll stop checking for people on hitbox
  173.         PunchHitBox.enabled=false;
  174.         Punching=false;
  175.         gameObject.rigidbody.drag=0;
  176.         //Clear the List of people that got punched on this punch.
  177.         BeingPunched.Clear();
  178.     }
  179.    
  180.     //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
  181.     void OnTriggerStay(Collider other) {
  182.         //If we're not punching, forget about it, just stop right here!
  183.         if(!Punching) {
  184.             return;
  185.         }
  186.         //If we are punching, and the tag on our trigger zone has a RigidBody and it's not tagged Player then...
  187.         if (other.attachedRigidbody&&other.gameObject.tag!="Player"&&(other.gameObject.tag=="Pushable"||other.gameObject.tag=="Enemy"||other.gameObject.tag=="Pickup")) {
  188.             //If this guy on our trigger zone is not on our List of people already punched with this punch
  189.             if(!BeingPunched.Contains(other.gameObject)) {
  190.                 //Call the DealDamage script telling it to punch the hell out of this guy
  191.                 Camera.main.GetComponent<CameraShaker>().DoTheShake(regularpunchshake*2,regularpunchshakeamount*2,regularpunchdecfactor/2);
  192.                 PunchFX.ActivateCodeTriggeredEvent2();
  193.                 DoDamage.Attack(other.gameObject,PunchDamage,PushHeight,PushForce);
  194.                 SlowMotion();
  195.                 //Add him to the list, so we won't hit him again with the same punch.
  196.                 BeingPunched.Add(other.gameObject);
  197.             }
  198.         }
  199.     }
  200.    
  201.     //This will return a TRUE/FALSE on the animation we want to check if is playing.
  202.     bool CheckIfPlaying(string Anim,int Layer) {
  203.         //Grabs the AnimatorStateInfo out of our PlayerMove animator for the desired Layer.
  204.         AnimatorStateInfo AnimInfo = playerMove.animator.GetCurrentAnimatorStateInfo(Layer);
  205.         //Returns the bool we want, by checking if the string ANIM given is playing.
  206.         return AnimInfo.IsName(Anim);
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement