I’d like to know this as well - can we fetch the animator assigned to a particular state? For instance, if I have a state “Running” with an animation clip “run”, is there a way to:
Animation is deprecated, Animator knows about current and next state … so WHY is Animation deprecated when it’s only way how to do what i need and Animator does not look to be doing it in near future …
sure there is a way, you can export all states in editor with all info, but it’s so uncomfortable and very dirty solution if you are working in team
so if anybody knows please help us
Edit: i need to play animation by Play and CrossFade i don’t use transitions, so there is not next state
I’d be interested in a solution to this problem as well. In my case I need to get the length of an animation from the animator on the server side where the animator components are disabled. That means I can’t use anything related to the current state.
Currently the animation duration is hardcoded for each animation but that’s not a real solution, it’s a very dirty workaround for what should have been fixed in the API long ago. We need something like what has been suggested a few posts above mine already:
You can use the internals if you dont mind using undocumented code:
RuntimeAnimatorController controller = GetComponent<Animator>().runtimeAnimatorController;
if (controller is UnityEditorInternal.AnimatorController)
{
UnityEditorInternal.StateMachine m = ((UnityEditorInternal.AnimatorController)controller).GetLayer(0).stateMachine;
for (int i = 0; i < m.stateCount; i++)
{
// Obviously loading it depends on where/how clip is stored, best case its a resource, worse case you have to search asset database.
AnimationClip clip = (AnimationClip)Resources.LoadAssetAtPath("Assets/Animations/" + m.GetState(i).GetMotion().name + ".anim", typeof(AnimationClip));
if (clip)
{
Debug.Log(clip.name + ": " + clip.length);
}
}
}
The problem with the Internals is that they aren’t available in the relase version of you’re game. They are just accessible if you run you’re game in the Editor.
True but if you are working in release/runtime it seems simple strageies like putting the animation clips in a predictable place or using an info file would cater for most use cases. If your clips aren’t changing then its not really an issue and if they are changing its very likely those changes need to go through the Editor where you could use the Internals classes to do any automatic packing/validaiton/file writing/etc.
PS I’m not saying such access woulnd’t be good but that in many cases a workflow can be created to solve the problems. For example @blitzprog could use a “hard coded” info file and use the internals approach to write this file every time a build is created.
While this is true and certainly works for most applications, it doesn’t solve the problem for situations where you don’t play the animation.
In my case the animations are available on the server side as objects but they are not played. For this scenario it would still be very handy to have an API like this.
It’s increasingly apparent while adjusting to new systems that Unity has a habit of taking two steps forward and one step back. All of the great features with Mechanim, and yet we can’t do something as simple as get the length of an animation?
I found a pretty simple solution, I’m not sure how it will work for every problem but you can still get the animation clip length directly by referencing the clip it self (Not through the state machine).
Here we have an attack animation that is played in the Animator state machine by setting a bool to true, then using a coroutine we wait until animation has finished and then switch the bool back to false.
In this implementation it is required that you set the ‘attack’ animation in the inspector to reference it, however if your animations are in the Resources folder then you can use the Resources class to load it in via script. Hope this helps.
public class PlayerController : Monobehaviour {
public AnimationClip attack;
Animator _animator;
void Start() {
_animator = this.GetComponent<Animator>();
}
void Update() {
if(Input.GetButtonDown("Fire1")) {
StartCoroutine(Attack());
}
}
IEnumerator Attack() {
_animator.SetBool("Attack", true);
yield return new WaitForSeconds(attack.length);
_animator.SetBool("Attack", false);
}
}
how would one wait till the animation has finished once and then launch a projectile ate lets say an oportune time during said anmation ?would this code be the solution if i tweaked it or is there a nicer way ?
So I tried this…and it doesn’t work. I created a dummy class that had references to the basic animations that every character in my game will have, and then I have a method where I input an enum value and get the respective animation’s length. I always get 0. No matter what. Everything is referenced correctly and it is the animations that are used in the state machine. I’ve set up default returns (in the case that some reference is missing) to 1.0f, but I never get that, implying that the animations are read and the animationClip.length is actually 0.0f. But this makes no sense, as even a single frame animation would be greater than that.
Edit: Added a debug output of the AnimationClip.ToString(), and I get the correct animation name. So everything is correct but the clip’s length is 0, which makes no sense as it is an actual animation.
Edit 2: Nevermind, it works, I made a silly elsewhere (irrelevant to the topic so no details).
Hey… its a bit late, but i found a kind of solution for this… You must declarate a “public animationClip” in your code, then make the reference to the clip from the inspector dragging the animation file into it.
Then simply use “clip.lenght” for example to get the duration of the clip.
It’s the only way i found to do it.
This thread is about Mecanim animations being affected by speed, not animations in general.
What you’re talking about only gives us the base clip length, not the real length used in Mecanim.