Measuring time between hits to apply COMBO ATTACK animations

60906-20151230-152648.png

Hi.
My simple game project is about running and breaking walls.

I have PlayerCtrl script that manages player animations and WallCtrl script that detects hits.

I’m using Animator.Setbool() to switch between animations

I’m trying to apply different hitting animations only when a player hits walls continuously in short time(combo hits).

I googled and they say measure time between hit and hit but I need little more advice.

can anyone tell me how to measure time between two wall hits?

and some tips about what to do after time measured would be very appreciated.
here’s PlayerCtrl script and WallCtrl script below.

thank you so much for reading.

public class PlayerCtrl : MonoBehaviour {

	public float moveSpeed;
	public float jumpForce;
	public bool isAttackDone = true;
	public GameObject hitCollider;
	public GameObject hitColliderPos;

	private Rigidbody2D myRigidbody2D;
	private Animator anim;


	// Use this for initialization
	void Start () {

		myRigidbody2D = GetComponent<Rigidbody2D>();
		anim = GetComponent<Animator>();
		isAttackDone = true;
	
	}
	
	// Update is called once per frame
	void Update () {

		//Debug.Log ("isAttackDone = " + isAttackDone);

		myRigidbody2D.velocity = new Vector2(moveSpeed, myRigidbody2D.velocity.y);

		if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
		{
			if (isAttackDone == true)
			{
				GameObject hitColl = (GameObject) Instantiate (hitCollider, hitColliderPos.transform.position, hitColliderPos.transform.rotation);
				anim.SetBool("Attack", true);
				hitColl.transform.parent = GameObject.FindGameObjectWithTag("PLAYER").transform ;
				Debug.Log("PUNCH!!");
				isAttackDone = false;
			}
		}
	
	}

	public void isAttackDoneTrue()
	{
		isAttackDone = true;
		anim.SetBool("Attack",false);

	}

	public void DestroyHitCollider()
	{
		Destroy (GameObject.FindGameObjectWithTag("HIT"));
	}

	public void Die()
	{
		anim.SetBool("Die", true);
	}

}

public class WallCtrl : MonoBehaviour {

	private BoxCollider2D wallCollider;
	private bool isGameOver = false;
	private Animator wallAnim;
	private UImgr uiMgr;
	private PlayerCtrl playerCtrl;
	private PlatformCtrl platformCtrl;

	// Use this for initialization
	void Start () {
	
		wallCollider = GetComponent<BoxCollider2D>();
		wallAnim = GetComponent<Animator>();
		uiMgr = GameObject.FindGameObjectWithTag("CANVAS").GetComponent<UImgr>();
		playerCtrl = GameObject.FindGameObjectWithTag("PLAYER").GetComponent<PlayerCtrl>();
		platformCtrl = GameObject.FindGameObjectWithTag("PLATFORM").GetComponent<PlatformCtrl>();
	}
	
	// Update is called once per frame
	void Update () {
	



	}

	void OnTriggerEnter2D (Collider2D other)
	{
		if (other.tag == "HIT")
		{
			if(isGameOver == false)
			{
				wallAnim.SetBool("Break", true);
				Debug.Log ("HIT!!!");
				wallCollider.enabled = false;
				uiMgr.ScoreUp();

				//Destroy (other.gameObject);
			}
		}
	}

	void OnCollisionEnter2D (Collision2D coll)
	{
		if (coll.collider.tag == "PLAYER")
		{
			isGameOver = true;
			playerCtrl.Die();
			platformCtrl.moreWalls = false;
			Debug.Log("GAME OVER");


		}
	}

	public void DestroyWall()
	{
		Destroy (this.gameObject);
	}
}

The key is knowing that you use Time.time to get current time in an Update() function using:

At line 11, add:

private float lastHitTime; // Place to save last hit time

At line line 96, or so add:

lastHitTime = Time.time; // Save the current time of the last hit.

Wherever you want to see how long it’s been (in seconds) since the last hit, you can do:

float timeSinceLastHit = Time.time - lastHitTime;
if( timeSinceLastHit < 0.2f)
{
   // Maybe you want to do a double hit if the user hits twice in .2 seconds, for example.
}