- Home /
Animator state stuck
I have set up a button that triggers a sword slash animation. It works as intended but when you spam click the button the animator state gets stuck. It continues working after I update it in the inspector or pick up a different object but that obviously is not a desirable solution. I've tried to limit the press of the button with different delays or cooldowns for the animation trigger but nothing has worked so far.
My guess is that the "slash" animation gets triggered again before it finishes and that causes it to get stuck. I also tried changing exit times, animation speed and transition length between the states but I wasn't able to fix it. Any suggestion would be appreciated. Thanks in advance.
what is the condition for the animation to be activated? Trigger?
It's a bool that turns true when the button is pressed and it turns false by an event trigger at the end of the animation.
Are you using?
if (Input.GetKeyDown(KeyCode."used key"))
{
print("was pressed");
}
if it's an action that's used every time you press a button you can use trigger, so it's only called once
Answer by DEGUEKAS · Mar 19, 2020 at 01:18 PM
first we create a parameter that is of type Trigger in the animator.
now to get to the Slash animation in the conditions we put Slash Trigger
2. second, to return to "new state" we do not put parameters just exit when the animation is finished for that the only thing that we will activate is the variable "Has Exit Time"
this is the code you need:
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
animator.SetTrigger("Slash");
}
}
what it does is access the animator and when we click activate the variables "Slash" which is of type trigger.
to activate the animation we need it to be true but to get out no, it will just come out of the animation at the end of your time.
using bool for a simple action on the animator may be unnecessary and let's also remember the mouse inputs.
"Input.GetMouseButtonDown" will only be true when we press it and for it to return to true we have to release the click to press it again.
"Input . GetMouseButton" will be true if you click and leave it pressed, I don't think it's what you're looking for and finally we have "Input.GetMouseButtonUp" which is true when you stop pressing the click.
pages with more information: https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonUp.html
https://www.youtube.com/watch?v=gON_hhhvheI
https://www.youtube.com/watch?v=0t3AyFg2fh8
I'll try that out, If it works I'll accept the answer. Thanks a lot for your help.
Answer by Shamantiks · Apr 28 at 09:32 AM
Great call! My animations, once in a blue moon would get stuck. This was the reason, I had a bool "exit on false" set, with exit time. I removed the extra bool parameters, and now I don't get stuck! Nice one!!
Your answer
Follow this Question
Related Questions
how to make multiple transition from one state? 1 Answer
How do I access the States of an AnimationController with c#? 1 Answer
Why is my char animation not showing on my opponent's Photon window? 1 Answer
How can I make a state pass after a reversed state? 0 Answers
Animations - Can you cut them? 2 Answers