I got a character that got the ability of releasing skill(with animation) and run away(moving back),I use statemachine to mark the skill-release state and the moving state, given the releasing animation got its own duration itself,so what is the best way to set the status?
Maybe I could got the animation length ahead and restore it to a fixed value,but I dont think it is flexible for variable game situation,for instance,a interuppt ocurred when hited by the enemy. So what is the best way to handle this under the unity mechanism animation ?
I use Animancer so no steps to share but look up animation events, it’s what you are looking to find.
AnimationEvents and / or Animator State Machine Behaviours are two possible ways.
AnimationEvents:
Important implementation detail about AnimationEvents:
Be careful with animation events! I once attempted to use an animation event on the end of my attack animation to detect when a character finishes attacking so the character could resume walking. It did not work reliably, though, and sometimes my character would get stuck. I spent a long time to debugging this and never found out what the problem was. The only thing that I can figure is that my state-machine logic must have allowed the state to change before the animation was complete in some cases, so then the event was never reached.
Now I only use animation events for noncritical things like triggering particles or footstep sound effects.
I’ll take it a step further and suggest never using animation events at all. As other said already they are quite unreliable and on top of that they generate garbage. My personal suggestion is to have your system dictate how long to play the animation and then time out when to trigger any secondary effects. This has the added benefits of not trying your logic to Unity’s Animator and allowing your logic to run consistently even without the provenance of animations being run (which can come in handy when you start doing networking or multiple background simulations)
I went back to that old project to see how I had solved it previously. It looks like I checked for this in Update:
if (!animator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
as soon as the IsTag returns false then that means the attack animations are over. You just need to set the tag in the animator Controller. This method has it’s own “gotchas” though. For example, In my game (with input buffering) it could play several attack animations in row. So in that case, it doesn’t detect when each attack animation ends. It will only finally be true when some non-attack animation plays.
What about Animator State Machine Behaviours? What are the pitfalls of this method? Where can I see a good implementation?