- Home /
Animator.GetCurrentAnimatorStateInfo(0).IsName("FallOffBed") unexpectedly returns false
Hi, I am using Animator.Play to play animation, then want to measure exact time of this clip, before executing next line in a coroutine.
I checked other answer here, but tried implement with no success so far as follows:
priestAnimator.Play("FallOffBed");
int baseIndex = priestAnimator.GetLayerIndex("Base");
print("baseIndex is " + baseIndex);
print("long expression IsName FallOffBed is " + priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).IsName("FallOffBed"));
print("long expression IsName Idle is " + priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).IsName("Idle"));
print("long expression IsName Base.Idle is " + priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).IsName("Base.Idle"));
print("long exp normalizedtime is " + priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).normalizedTime);
print("both concat with andand is " + (priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).IsName("Base.FallOffBed") &&
priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).normalizedTime < 1.0f));
while (priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).IsName("Base.FallOffBed") &&
priestAnimator.GetCurrentAnimatorStateInfo(baseIndex).normalizedTime < 1.0f)
{
yield return null;
}
// above NOT producing delay or doing anything now! so measure length animation clip at moment guess 4 seconds
yield return new WaitForSeconds(4);
My console output from above debugging lines is:
baseIndex is 0
long expression IsName FallOffBed is False
long expression IsName Idle is True
long expression IsName Base.Idle is True
long exp normalizedtime is 1.919069
both concat with andand is False
My animation plays fine, I have Write Defaults off so it keeps same position after playing once. I'm new still, so confused a bit what is going on and how GetCurrentAnimatorStateInfo
and normalizedTime
are working here exactly?
Another answer here from 2015 suggests using animation.clip.length
seems simpler, which I think means adding an Animation component to the object to get the reference to animation, but this is legacy component as I understand, so not really supposed to do that now? I am willing to spend time learning 'proper' way of doing things if possible!
NB I hacked with yield return new WaitForSeconds(4);
for now as you can see, but this is not proper way, thanks.
Hi, sorry if comment is overdetailed but in case clearer, this seems to indicate animation is in the State Idle
after executing priestAnimator.Play("FallOffBed")
- but from other answer on here, and also my own expectation, I was expecting animation to be in the state FallOffBed
as soon as this line is executed - I see here normalizedTime
is returning the number of times current animator state has played (which here is returning the number of times Idle state played before priestAnimator.play executed, so here it indicates Idle has played 1.9 times (I pressed keys to execute this coroutine pretty quickly after game started). So I think the key problem might be my misunderstanding somewhere about GetCurrentAnimatorStateInfo
- honestly grateful for any comment, suggestion or idea thanks, I'm a beginner!
Your answer
Follow this Question
Related Questions
How do I make the transition between two states in the "Animator" happen instantly? 3 Answers
How to get fileID information so i can just edit Mecanim Animator through script? 2 Answers
Animator does not play Animation on object 1 Answer
Easier way of creating transitions in the animator? 1 Answer
Is it possible to make empty, passing state in mecanim? 3 Answers