Although I can look up the base length of whatever clip is currently being used by a state via animator.runtimeAnimatorController, I cannot account for the state’s various speed multipliers that will affect the final clip length.
The multipliers are available in AnimatorStateInfo, but Unity only gives us GetCurrentAnimatorStateInfo and GetNextAnimatorStateInfo. I would like to be able to access this information for any state in the animator. I was hoping for something like GetAnimatorStateInfo(string stateName) but I haven’t found anything like that in the documentation. Is there another way?
CONTEXT:
In my 2D game, the player’s animator controller has some fixed set of states (idle, move, action, etc.).
I use AnimatorOverrideController to change the clips associated with these states dynamically at runtime, e.g. equipping a tool changes the clip associated with the “action” state. This simplifies the animator controller and allows for things like changing the players visible equipment.
I use speed parameters in the animator controller to adjust the speed of a state dynamically for things like movement speed, but I’m also adjusting the state’s actual speed as a convenient way to fine tune the base speed of the animation clips.
Hard coding the speed values is out, and I would also like to avoid changing the frame rate of the clips themselves in the animation view as this is very slow and difficult to make tweaks. Thanks in advance!
you cannot do it at runtime, the final length of a state can be very hard to compute manually, a state can containt only one animatio clip but also in a state you can have a blend tree with many animation clips of different lenght, depending on the blending factor and the speed multiplier the final length of the state is impossible to compute without actually doing the blend.
It sounds like what I want is not possible and I may have to just edit the animation clips. I do change various speed parameters at runtime, but that’s no problem because I always know the value associated with the parameter (and can multiply that with the clip length). The thing I don’t know is the plain old “Speed” of each state (which I’m not changing at runtime, but using to adjust the clips associated with the state):
This screen shot is not from my game, but this shows the value that I want to access at runtime for any state.
I’m adjusting this speed value in the inspector, but not at runtime. I suppose I could make a function that cycles through each state and records the speed value at start up, but that seems kinda inelegant.
It’s sounding like I should really just be adjusting my actual animation clips…it’s just so much easier to adjust the speed value to some float instead of nudging around the key frames in my sprite animations.
Sorry for the delayed response. I solved my problem by always leaving the state speed at 1 and adjusting all of my animation clips. I’m making sure to only modify a state’s speed using speed multipliers now. When I started work on the project I just assumed incorrectly that the value I entered in the inspector for state speed would be accessible for any state.
The reason why I wanted to know the final speed of a clip before I played it was that I have some scripts that trigger different events based on the normalized time of the animation playing, and there are cases (mostly for the AI) where I want to simulate the results of some animation without actually playing it.
the fact that unity doesn’t allow access to speed multiplier is just BS.
Speed multiplier becomes useless in some cases and a source of errors, which messes with the actual parameter multiplier we set at runtime.
If it’s impossible to provide at runtime, why not add a checkbox to be able to disable speed in the editor (so that it doesn’t mess with the parameter multiplier)?