I’m trying to code an animation where the character fires a bow and then returns to an idle animation. But the character never returns to the idle animation.
I have a parameter called “isShooting” which plays the animation when the player clicks the left mouse button. I want the animation to play once and then return to the idle.
Void OnFire( InputValue value)
{
If(value.isPressed)
{
myAnimator.SetBool(“isShooting”, true);
}
Instantiate(arrow, bow.position, transform.rotation);
}
To have the animation play once and then return to idle, you will need to add a transition from your shooting state back to idle. If you set the exit time to 1, then the animation will play one time before transitioning back.
(In this image, consider “SecondIdle” to be your shooting state)
However, if your isShooting bool is still set to true when you get back to idle, then the animator will transition back to your shooting animation immediately. You may want to use an animation Trigger rather than a bool. Triggers are like instantaneous events that will only be true for one frame.
One last thing to consider is that it looks like your code to instantiate an arrow will occur whenever OnFire is called. This means the player can rapidly press the button to fire arrows out of sync with the animation. Depending on whether or not this is desired, you may need to add in some sort of cooldown or lockout period.
You are never calling SetBool("isShooting", false);