- Home /
Question by
Antoine_L · Sep 01, 2018 at 11:21 PM ·
colliderswaitforsecondsienumeratorenable and disable script
Enable collider then disable it after a certain amount of time
Hi All, I am making an attack animation for my game and when you attack, I want it to enable a collider for 0.5f seconds. I have a script that enables it but I am not sure how to disable it after 0.5 seconds... Heres what I have so far
void Update () {
if (Input.GetKeyDown(KeyCode.J))
{
GameObject.Find("player").GetComponent<Animator>().Play("Attack");
m_Collider.enabled = true;
animator.SetTrigger("attacked");
//Wait for 0.5f seconds
m_Collider.enabled = false;
}
}
thanks, Larry
Comment
Best Answer
Answer by dspruill · Sep 02, 2018 at 01:30 AM
To wait for half a second, you can do it in an IEnumerator, here's what I would put instead of your comment
StartCoroutine(waitForSec(.5f));
And down below have a coroutine that looks something like
private IENumerator waitForSec(float sec){
yield return new WaitForSeconds(sec);
m_Collider.enabled = false;
}
Cheers! (you can also hard code the time into the method, it's just good practice to keep it like that ;)