Stop animation after .5 seconds?

Hi there!

When pressing E, the player starts an Attack animation. That needs to stop after 0.5 seconds.

How do I do that?

The simple code looks like this:

		if(Input.GetKey(KeyCode.E))
		{
			anim.SetBool("Attack", true);
			Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
		}

Thanks

void Update(){

  if(Input.GetKey(KeyCode.E))
    {
    	anim.SetBool("Attack", true);
    	StartCoroutine(WaitForHalfASecond());
    	Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
    }
}
    
    IEnumerator WaitForHalfASecond(){
    	yield return new WaitForSeconds(0.5);
    	anim.SetBool("Attack", false);
    }

C#

If you have any questions, or would like an explanation on how to further use IEnumerators in this case, I will be happy to answer in the comments. IEnumerating is a difficult concept to grasp until you’ve done it a lot!