Search Unity

Is it possible to drive an animation using a float parameter?

Discussion in 'Animation' started by invicticide, Jan 11, 2014.

  1. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    I'm pretty new to Mecanim, so please bear with me. :p

    I have this jump animation clip which starts with both feet on the ground and a little anticipation before the character takes off, and ends with both feet back on the ground with the character's legs bent to absorb the shock of the impact. All very natural-looking.

    In Unity, the player's GameObject is moved through the jump arc via script, and the animation is played in-place (so basically I'm ignoring root motion... I think?) I'm calculating the jump height relative to the ground underneath the character at any given point in time, so I have this float which is 0.0 when the character is on the ground and 1.0 when he's at the apex of the jump, and it transitions smoothly in between.

    From this I can easily extrapolate another float that's basically "jumpPercent" which is 0.0 at the start of the jump and 1.0 at the landing point at the end of the jump. I'd like to send this jumpPercent float to Mecanim to drive the playback of the jump animation. That is to say, when the float is 0.0 the animation clip is at frame 0, and when it's 1.0 the clip is at its final frame. In effect this would compress the animation playback time to exactly match the amount of time the character spends in the air. (Specifically, I'm interested in getting that little bit of impact pose at the end of the animation to actually happen at the moment the character lands.)

    So far as I can tell, though, the only ways to actually use float parameters inside Mecanim are a) threshold conditions to enter/exit animation states, and b) to drive a blend tree. Neither of these are actually driving a particular animation clip.

    Is there something I'm missing? Or is there a different/better way to accomplish the behavior I described? I guess naively I could use a blend tree to go between three static poses -- on ground (start), in air (apex), and on ground (end) -- but that doesn't really let us animate any nuance throughout the jump; it just becomes this bland linear interpolation at that point.

    We could time-scale the animation in the import settings or in the source file itself easily enough, but we'd have to keep going back and doing that whenever we re-tune the jump physics in order to match things up (which will probably be often, for the foreseeable future at least), and that seems silly if I can avoid it by simply driving the animation with a float that I already have.

    Suggestions?
     
  2. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    Perhaps you could make the land/recoil a separate animation, and blend towards it from the falling animation. You could drive that blend with a parameter in the Animator.