- Home /
Coroutines and animation triggers
Dear all,
I am currently working on a 2D game, and thought it would be a good idea to trigger the attack animations in coroutines, since I have several different attack types, and hence different times.
To do that I first launch the coroutine from the Update function when the attack-key is down:
void Update (){
if(Input.GetAxis("attack")>0.5f&&!hit)StartCoroutine("AttackManagement");
}
Then in the coroutine, I first start the attack animation and set a bool to true (means I'm in the coroutine, I can't currently start a new one), then wait for some time (this is the part that waits for the animation to play), then launch the idle animation, again wait for some time, and finally set the bool to false (acts as cooldown) :
IEnumerator AttackManagement(){
hit=true;
anim.SetTrigger("attack");
Debug.Log("got here");
yield return new WaitForSeconds(weaponTimes[equipedW]);
Debug.Log("got here2");
anim.SetTrigger("idle");
Debug.Log("got here3");
yield return new WaitForSeconds(weaponCoolDown[equipedW]);
hit=false;
}
And here comes the problem: the 3 debugging messages appear in the console, but the animation is not triggered. It either gets stuck in the "attack" state, or in the "idle" state, but does not behave the way I want.
In the animator I have different layers. Whats really strange, is that the trigger seems to work on some layers, but not on others.
Thank you a lot for your help :)
I did, and I am pretty sure I understand it correctly. I first implemented the same code as a finite state machine, and it worked, but with multiple Time.time calls, so I thought it would be easier to implement it as a coroutine. So my guess is the problem comes from my possible missuse of coroutines, not from the setTrigger function.
Here I am basically just using the 2 triggers as booleans. I could as well have used a bool and set it to true and then false.
Thanks for your answer anyway.
Indeed, path of least resistance is the best approach with Animator. I was indirectly checking you were referencing the name of a Parameter rather than a state or animation. Usually, for Parameters I give them names like "idleBool" or "walkFloat" to help me visualise the distinction when coding it.
To handle your stuck states, make sure there are appropriate transitions in your State $$anonymous$$achine (Animator window) leading from the Current to Go-To states as well as away from them. Decide which transitions require a bool and which can function from Exit Time.
Thanks for the nomenclature idea, I will definitely use that from now on.
For the second part: I think the states are implemented correctly. They do work in some layers, but the strange thing is that the parameter is not activating in all layers. Is it possible to deactivate a parameter for a given layer? $$anonymous$$aybe I did that by inadvertence. When I call setTrigger from the Update function the information is passed to all the layers, it's only in the coroutine that the information seems lost somwhere.
Thanks for your help so far :)
Answer by RedHillbilly · Jun 26, 2014 at 10:06 AM
I replaced the two trigger parameters by one boolean parameter that takes the states true and false, and it works now. I don't now why it wasn't working with the triggers, the mechanism should be the same. A bug related to coroutines maybe?
@meat5000 thanks a lot for your help, I should have tested that from the start, sorry that I wasted your time.