Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using PigeonCoopToolkit.Effects.Trails;
- public class PlayerMelee : MonoBehaviour {
- //We'll use those 3 to communicate with the rest of the kit.
- private PlayerMove playerMove;
- private CharacterMotor characterMotor;
- private DealDamage DoDamage;
- //This will be used to cycle through 2 different punch animations
- private bool punchleft;
- //This will be used to check if we should ge giving damage.
- private bool Punching;
- private List<GameObject> BeingPunched = new List<GameObject>();
- //These are our public vars, PunchHitBox should be like the GrabBox,
- //and should encompass the area you want your punch to cover
- public BoxCollider PunchHitBox;
- public int PunchDamage = 1;
- public float PushHeight = 4;
- public float PushForce =10;
- //the factor used to slow down time
- public float slowFactor = 4f;
- //the new time scale
- private float newTimeScale;
- //the factor used to slow down time
- private int FramesToGetBack = 0;
- public int WaitFramesSlowMotion=4;
- public float PunchDrag=10f;
- public Trail LeftHandTrail;
- public Trail RightHandTrail;
- public float regularpunchshake;
- public float regularpunchshakeamount;
- public float regularpunchdecfactor;
- public TriggeredSpawner PunchFX;
- // Use this for initialization
- void Start () {
- //We're supposed to be on the same gameobject as the PlayerMove,
- //CharacterMotor etc, so lets get them as reference!
- playerMove = GetComponent<PlayerMove>();
- characterMotor = GetComponent<CharacterMotor>();
- DoDamage = GetComponent<DealDamage>();
- //Did you even make a PunchBox? Or you were lazy and didn't make one?
- if(!PunchHitBox)
- {
- GameObject GameObjectPunchHitBox = new GameObject();
- PunchHitBox = GameObjectPunchHitBox.AddComponent<BoxCollider>();
- GameObjectPunchHitBox.collider.isTrigger = true;
- GameObjectPunchHitBox.transform.parent = transform;
- //Let's place it a little but farther away from us.
- GameObjectPunchHitBox.transform.localPosition = new Vector3(0f, 0f, 1f);
- //It should Ignore Raycast so let's put it on layer 2.
- GameObjectPunchHitBox.layer = 2;
- Debug.LogWarning("You were too lazy to make a PunchHitBox so I made one for you, happy?", GameObjectPunchHitBox);
- }
- //Also lets turn off our PunchHitBox, we'll only turn that on while punching
- //so the Grabbing script doesn't get confused with it.
- PunchHitBox.enabled=false;
- }
- // Update is called once per frame
- void Update () {
- //If frames to get back is bigger than 0
- if(FramesToGetBack>0) {
- //Then decrease frames to get back by 1
- FramesToGetBack--;
- } else {
- //If frames to get back IS 0 and our timescale is different than 1.
- if (Time.timeScale != 1.0f)
- {
- //Then let's stop our slowmotion.
- StopSlowMotion();
- }
- }
- if (Input.GetButtonDown ("Punch")) {
- //First off, let's check if we're not already punching, see if the punch animations are playing.
- if(!CheckIfPlaying("RightPunch",2)&&!CheckIfPlaying("LeftPunch",2)) {
- //If I got here no punch animations are playing so we can punch now :D
- if(punchleft) {
- //Tell the anim ator to play the left punch, and change the bool so next punch will be right punch!
- playerMove.animator.Play("LeftPunch",2);
- LeftHandTrail.Emit = true;
- punchleft=false;
- //Then start the coroutine so the punch effect happens when the animation already reached the interest part.
- gameObject.rigidbody.drag=PunchDrag;
- StartCoroutine(WaitAndPunch());
- } else {
- playerMove.animator.Play("RightPunch",2);
- RightHandTrail.Emit = true;
- punchleft=true;
- gameObject.rigidbody.drag=PunchDrag;
- StartCoroutine(WaitAndPunch());
- }
- }
- }
- }
- //This function stops slowmotion
- void StopSlowMotion() {
- //Set our timescale to 1
- Time.timeScale = 1.0f;
- //multiply our fixeddelta time by our slowFactor
- Time.fixedDeltaTime = Time.fixedDeltaTime*slowFactor;
- //Multiply our maximum delta time by our slowFactor
- Time.maximumDeltaTime = Time.maximumDeltaTime*slowFactor;
- }
- //This funciton will start our slow motion
- void SlowMotion() {
- //if the game is running normally (Timescale 1)
- if (Time.timeScale == 1.0f)
- {
- //assign the 'newTimeScale' to the current 'timeScale'
- Time.timeScale = newTimeScale;
- //proportionally reduce the 'fixedDeltaTime', so that the Rigidbody simulation can react correctly
- //dividing the fixedDeltatime by slowFactor
- Time.fixedDeltaTime = Time.fixedDeltaTime/slowFactor;
- //The maximum amount of time of a single frame, figured out by dividing MaximumDeltaTime by the slowFactor
- Time.maximumDeltaTime = Time.maximumDeltaTime/slowFactor;
- //Set our FramesToGetBack to be our WaitFramesSlowMotion. This will be decreased by 1 every frame until 0, then it'll get back to normal.
- FramesToGetBack=WaitFramesSlowMotion;
- }
- }
- IEnumerator WaitAndPunch()
- {
- //Wait for 0.12f time and then punch them in their stupid faces!
- yield return StartCoroutine(Wait(0.12f));
- PunchThem();
- }
- IEnumerator WaitAndStopPunch() {
- //Wait for 0.1f time before stopping the punch
- yield return StartCoroutine(Wait(0.1f));
- StopPunch();
- }
- //Coroutine for cool waiting stuff
- IEnumerator Wait(float duration)
- {
- for (float timer = 0; timer < duration; timer += Time.deltaTime)
- yield return 0;
- }
- void PunchThem() {
- PunchFX.ActivateCodeTriggeredEvent1();
- //Debug.Log("PUNCH!");
- //Enable our cool Punching Hitbox to check for enemies in there.
- PunchHitBox.enabled=true;
- //Turn our Punchin bool to true so our TriggerStay will check for people being punched.
- Punching=true;
- Camera.main.GetComponent<CameraShaker>().DoTheShake(regularpunchshake,regularpunchshakeamount,regularpunchdecfactor);
- //Start the coroutine that will wait for a moment and stop the punching stuff turning bools back to false.
- StartCoroutine(WaitAndStopPunch());
- }
- void StopPunch() {
- LeftHandTrail.Emit = false;
- RightHandTrail.Emit = false;
- //Turn stuff back to false so it'll stop checking for people on hitbox
- PunchHitBox.enabled=false;
- Punching=false;
- gameObject.rigidbody.drag=0;
- //Clear the List of people that got punched on this punch.
- BeingPunched.Clear();
- }
- //This function runs for each collider on our trigger zone, on each frame they are on our trigger zone.
- void OnTriggerStay(Collider other) {
- //If we're not punching, forget about it, just stop right here!
- if(!Punching) {
- return;
- }
- //If we are punching, and the tag on our trigger zone has a RigidBody and it's not tagged Player then...
- if (other.attachedRigidbody&&other.gameObject.tag!="Player"&&(other.gameObject.tag=="Pushable"||other.gameObject.tag=="Enemy"||other.gameObject.tag=="Pickup")) {
- //If this guy on our trigger zone is not on our List of people already punched with this punch
- if(!BeingPunched.Contains(other.gameObject)) {
- //Call the DealDamage script telling it to punch the hell out of this guy
- Camera.main.GetComponent<CameraShaker>().DoTheShake(regularpunchshake*2,regularpunchshakeamount*2,regularpunchdecfactor/2);
- PunchFX.ActivateCodeTriggeredEvent2();
- DoDamage.Attack(other.gameObject,PunchDamage,PushHeight,PushForce);
- SlowMotion();
- //Add him to the list, so we won't hit him again with the same punch.
- BeingPunched.Add(other.gameObject);
- }
- }
- }
- //This will return a TRUE/FALSE on the animation we want to check if is playing.
- bool CheckIfPlaying(string Anim,int Layer) {
- //Grabs the AnimatorStateInfo out of our PlayerMove animator for the desired Layer.
- AnimatorStateInfo AnimInfo = playerMove.animator.GetCurrentAnimatorStateInfo(Layer);
- //Returns the bool we want, by checking if the string ANIM given is playing.
- return AnimInfo.IsName(Anim);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement