I need to restart a firing animation if the firing animation is already playing, and the player tries to fire again.
I’m trying to use a null state to restart the animation, but the Fire state check sometimes fail, even though the the state machine is in the Fire state, resulting in the “isRestarting” variable not to be set to true.
How should this be implemented?
void FixedUpdate()
{
...
// Aiming
anim.SetBool("isAiming", weaponColtroller.IsAiming);
// Shooting
if (isFiring) // isFiring is set to true by an event that is triggered when the player clicks the mouse
{
isFiring = false;
if (anim.GetCurrentAnimatorStateInfo(1).nameHash == Animator.StringToHash("Aiming Firing Layer.Fire")) // NOT WORKING
{
anim.SetBool("isRestarting", true);
Debug.Log("DOUBLE TAP");
}
anim.SetBool("isFiring", true);
}
if (anim.GetCurrentAnimatorStateInfo(1).nameHash == Animator.StringToHash("Aiming Firing Layer.Null"))
{
anim.SetBool ("isRestarting", false);
}
if (anim.GetCurrentAnimatorStateInfo(1).nameHash == Animator.StringToHash("Aiming Firing Layer.Fire"))
{
anim.SetBool ("isFiring", false);
}
...
}
The state machine:
- Aim → Fire when isFiring is true.
- Fire → Aim when isRestarting is
false and on exit time. - Fire → Null when isRestarting is
true. Instant. - Null → Fire when isRestarting is
false. Instant.