Search Unity

Solutions for per-state animation speed control in Mecanim? (blend trees problematic)

Discussion in 'Editor & General Support' started by Lastair, Feb 3, 2013.

  1. Lastair

    Lastair

    Joined:
    Mar 16, 2012
    Posts:
    13
    I am trying to scale the speed of a walking animation to match the speed of movement in Mecanim like what was possible with legacy animation ('animation["foo"].speed = 0.5f' or such), but am not finding a good way to do it.

    Someone suggested blend trees that blend between the same animation at different timescales, but that has seemed problematic for me. For testing purposes I have a very simple blend tree controlled by a speed value from 0 to 10, with the speed=0 motion being at 0.01 timescale and the speed=10 motion at 1.00 timescale. However, the resulting interpolated timescales seem to be on a dramatic curve, even though the blue visualization graphic displays a simple linear crossfade. Speeds 0 and 10 work as expected but a speed value of e.g. 5 results in a timescale that looks more like a 0.1 (or even less!) than the ~0.5 it should logically be.

    Having found no options to influence this behavior, blend trees seem to not work as a workaround for my purpose. If anyone has any ideas on how to change this interpolation behavior with blend trees or other ideas for achieving the purpose of scaling a specific animation's speed (not using animator.speed, since that screws up other layers), help would be greatly appreciated.
     
  2. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    The Mecanim guys made a quick tut for doing this with Curves (on my phone ATM so don't have the link - google for mecanim root motion with curves), however it does require Pro.
     
  3. Lastair

    Lastair

    Joined:
    Mar 16, 2012
    Posts:
    13
    Oh, I don't have Pro. It feels like what I'm trying to do should be very basic functionality for an animation system but I suppose they need to put the paywall somewhere. Though it does strike me as odd that you can't access a state's timescale in code.

    I did however realize that I can just add more motions into the blend tree for e.g. speed 0 -> 0.01, speed 1 -> 0.10, speed 2 -> 0.20 and so on up to speed 10 -> 1.00, but the problem persists for the interpolated values, resulting in a sort of "jagged" response. Realistically it should end up close enough most of the time (even though the hackiness bothers me on a theoretical level), but would having something like 10 or even 20 motions in a blend tree affect performance?
     
  4. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    Something weird with these forums, they don't always submit replies. Ok second attempt but I'll be brief; blends don't have any appreciable overhead (unless I expect if you have lots of nested blends).

    You can access the state of the Animator through http://docs.unity3d.com/Documentation/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html and control current playback speed through Animator.speed

    The official mecanim YouTube tutorial and download project contain examples of using StateInfo to monitor states, so might be worth checking out for a more comprehensive code based solution.