Search Unity

Get the clip length (accounting for speed multipliers) of any animation state at runtime

Discussion in 'Animation' started by lunoland, Aug 8, 2016.

  1. lunoland

    lunoland

    Joined:
    Aug 4, 2016
    Posts:
    11
    Hello all,

    I have also asked this question over at Unity Answers (http://answers.unity3d.com/question...ting-for-speed-multiplie.html#comment-1226807) and am patiently awaiting an answer, but figured I'd post here too for more visibility. I have searched the forums/docs for a way to do this and found a few out-of-date 4.x threads which suggest it isn't possible. I'm wondering if things have changed since then.


    ISSUE

    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!
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,674
    Hi lunoland,

    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's not clear to me if you need to tweak those value at runtime or in the editor
    but if you need to do it in the editor then you have everything needed to do it
    http://docs.unity3d.com/ScriptReference/Animations.AnimatorState-speedParameterActive.html
    http://docs.unity3d.com/ScriptReference/Animations.AnimatorState-speedParameter.html
     
  3. lunoland

    lunoland

    Joined:
    Aug 4, 2016
    Posts:
    11
    Hey Mecanim-Dev,

    Thanks for the reply.

    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.
     
  4. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,674
    And why do you need to know the speed of the clip before it get played?

    Like you know you have the speed and speed multiplier available on
    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo.html

    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo-speed.html
    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo-speedMultiplier.html

    Also you have the same information in statemachine beahviour callback
    http://docs.unity3d.com/ScriptReference/StateMachineBehaviour.OnStateEnter.html
    the second parameter is an AnimatorStateInfo
     
  5. lunoland

    lunoland

    Joined:
    Aug 4, 2016
    Posts:
    11
    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.
     
    theANMATOR2b likes this.
unityunity