- Home /
Question by
Eddie21uk · May 29, 2017 at 06:29 AM ·
scripting problem2d-platformer
Trying to sequence my animator with Coroutine.
Hi all, I'm trying to get my animation to be in sequence so i've added a Coroutine (time delay) but it hasn't worked and i'm unsure what i've done wrong? Any help would be appreciated. thanks
if (Input.GetAxisRaw ("Fire1") > 0)
{
myAnim.SetTrigger("shooting");
StartCoroutine ("waitThreeSeconds");
fireRocket() ;
}
}
IEnumerator waitThreeSeconds()
{
yield return new WaitForSeconds (5);
}
Comment
Best Answer
Answer by ShadyProductions · May 29, 2017 at 11:53 AM
{
myAnim.SetTrigger("shooting");
StartCoroutine (Wait(5));
fireRocket();
}
}
IEnumerator Wait(int seconds)
{
yield return new WaitForSeconds (seconds);
}
Should work.
thanks but it now only seems in sequence every few shots and not all of them
Answer by sleepandpancakes · May 29, 2017 at 02:58 PM
If you want to run methods in sequence rather than in parallel, then those methods need to be in a coroutine, and you need to "yield" for each one before you call the next one. Also, defining a new Wait() coroutine is redundant since WaitForSeconds() will do the job.
if (Input.GetAxisRaw ("Fire1") > 0)
{
StartCoroutine(_FireSequence());
}
IEnumerator _FireSequence()
{
myAnim.SetTrigger("shooting");
yield return new WaitForSeconds(5); //note the use of yield
fireRocket();
}
Your answer