- Home /
Trying to check what anim clip is currently running on object
I have an object with several animations attached to it in the animator. I'm setting up the flow, and things are going good, but I need set a variable based on what animation is playing, or a way to tell what one I'm in.
thought this was the way to go.. but maybe not?: (In my class)
private Animator anim;
private AnimationInfo animInfo;
anim = GetComponent();
Then I thought I could do this:
animInfo = anim.GetCurrentAnimationClipState(0);
if(animInfo.clip == "spin")
But I get an error on the GetCurrentAnimationClipState because it's an AnimationInfo[] and not just AnimationInfo
Maybe I'm doing it all wrong? Anyone have a suggestion for a few lines that will get me the clip name that's running that I can check against?
thanks in advance! -B
Answer by TonyLi · Jun 05, 2013 at 06:07 PM
Mecanim can blend animations on a layer -- for example, running forward + turning to create a running turn. So multiple animations can be playing at the same time.
If you absolutely know that only one animation is playing, you can just use AnimationInfo[0]. But it would probably be better to iterate through the entire return value.
If you want the animator state, see: http://answers.unity3d.com/questions/418854/getting-a-list-of-mecanim-states-in-animator.html
Thanks. I think I'm closer.. but still not there. So, I'm sure I'll be only running the 1 animation at a time. I'd gladly iterate through whatever needed. I'm apparently brain dead today, and not sure how to get there. Looking at the Animator, in the Base Layer, I'm wanting to get what clip I guess the transition or the clip from the controller. so Base Layer.animName is what I'm looking for, and just can't figure out how to get there. I'm able to set Parameters, and force transitions, but I need to know where I'm at in my controller in a script. Thanks again for your help, but any more examples/info would be greatly appreciated. going to grab some caffeine, maybe that will help!
Unity recommends a simple "control" layer on top of Animator to keep track of things like this. (See: http://docs.unity3d.com/Documentation/$$anonymous$$anual/$$anonymous$$ecanimPeformanceandOptimization.html) I agree. It gives you more flexibility, and also future-proofs your code against the possibility of multiple simultaneous animations.
This layer can simply get/set one or more animator controller parameters, or you could make it more elaborate. You might consider an enum for the current state of the layer. For example, a snippet might look like:
enum CharacterState { Idle, Walk, Spin }
private CharacterState state = CharacterState.Idle;
private void StartWalking() {
state = CharacterState.Walk;
animator.SetFloat("Speed", 1);
}
private void StartSpinning() {
state = CharacterState.Spin;
animator.SetBool("Spin", true);
}
if (state == CharacterState.Spin) {...}
But if you really just want the animation clip name, the easiest solution is to make sure your animation state name matches the clip name. This is the default when you drag an animation clip into the Animator Controller edit pane.
Animator animator = GetComponent<Animator>();
int currentClipHash = animator.GetCurrentAnimatorStateInfo(0);
int spinHash = Animator.StringToHash("spin");
if (currentClipHash == spinHash) {...}
(Warning: untested code)
Thank you for the info and help! Very informative. The reason I started this way is because I was going the enum route. So here is the situation. Animator starts up.. and I'm in my Idle Animation. And I set my state to CharacterState.Idle So then I want to do a spin.. so I get a keypress and call StartSpinning() just like you have above.
After my spin is done (transition on exit time) I go back to idle.
$$anonymous$$y problem is, state is still equal to CharacterState.Spin and my bool of "Spin" is still true. So the reason for my original question: I want to know when I'm in the spin animation, so I can set the Spin to false, and my state back to idle when I get there. I just don't know when that happens. I was hoping to know when my Spin animation was happening, so I can set my state and bools etc. Else I keep ping-ponging back to spinning because my transition sees Spin as true still.
Is there a better way I should do that? How does everyone else re-set their animator.Bools to false, or whatever and know when to do it? Thanks again!
Use the Event System for $$anonymous$$ecanim. I'm not the author, and I have no stake in plugging it, but it's more than worth the cost. You can just attach an event to the end of your spin animation to call some method like StopSpinning() or StartIdle().
Or, if you don't want to get the Event System for $$anonymous$$ecanim, you can do like the Unity-provided examples and check GetCurrentAnimatorStateInfo() in every Update() cycle (like the example code I posted above).