SOLUTION!!!
The above author might have made a typo. We do not want to overwrite OnStateEnter()
or OnStateExit()
; we want to overwrite OnStateMachineEnter()
and OnStateMachineExit()
. Put this in your new script and remember to define your boolean in the Animator state machine, and enter the name in the serialized field in the editor:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SubStateMonitor : StateMachineBehaviour
{
[SerializeField] string myBool;
override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
{
animator.SetBool(myBool, true);
}
override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
{
animator.SetBool(myBool, false);
}
}
You can do the same boolean check as before
(animator.GetBool("YourBooleanNameHereInQuotes")
);
ORIGINAL POST
So, I can test when I’m inside a state machine now but it doesn’t seem to recognize when I am inside a state anymore with (for example)
if (animator.GetCurrentAnimatorStateInfo(0).IsName("First_Punch"))
inside the state machine “Attack_Punch”
EDIT I have confirmed that when I am inside an animation state INSIDE a submachine, that inside the new behaviour script we wrote for the submachine, we automatically go through OnStateExit()
, which turns our new boolean to false
. This may be because I am already checking to see if I am inside another animation state (see IF statement above) and Unity can’t allow me to be in two animation states at once (or check for it, or perhaps it has something to do with indexing). During the transition between animations, the I can see that OnStateEnter() is ran again and the new boolean’s value is true
again.
So this is either an unfortunate oversight or (preferably) I am forgetting to do something when I run my existing code inside an IF statement that does
if (animator.GetBool("sm_Attack_Punch")){ //Existing code, if statements here}
.
Can somebody please clarify?
In the above state machine, if I attach the behaviour to Battle_JumpAttack
, check that I am inside that state machine, and provided that is true, check that I am inside the first state called Jump_Start_Beginning
, the current state will not be Battle_JumpAttack
anymore when I am running the first animation.