Search Unity

Perfectly timing a falling animation

Discussion in 'Animation' started by vitorcanonico, Nov 6, 2019.

  1. vitorcanonico

    vitorcanonico

    Joined:
    Oct 10, 2019
    Posts:
    4
    As far as I know, the easiest way to deal with jumping and falling animations is to make them simple loops toggled by a certain variable in code.

    In the Fancy Pants Adventures series of Flash games, however, this is not the case. Somehow, the Fancy Pants character knows precisely how long he'll take to reach the apex of the jump (which depends on how long the jump button was pressed), and times his jumping animation accordingly. Similarly, he appears to know precisely how long it'll take for him to reach the ground, and uses that to also time his falling animation. The result can be seen in the gif below.

    2019-11-05_23-10-22.gif

    Fancy Pants also has a surprisingly precise backflip move. It is somehow able to calculate how long the character will stay airborne (even though most of the terrain is decidedly not flat) and times the animation so that the character always does precisely a single backflip, no more.

    Interestingly, as can be seen in the gif below, there are times where this doesn't work. From what I can see, Pants appears to not consider the time to reach a particular piece of ground when it is of a certain angle, and can therefore bonk in it if the backflip animation hasn't finished yet.

    2019-11-05_23-25-24.gif

    As I said above, this was all done in Flash. How can one achieve a similar result in Unity? Is it all a matter of using physics equations to predict how long the animations should last, changing the Animator's speed accordingly?

    Thanks in advance.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, I'm not suggesting this will re-create this specific game's jumping logic, but for some situations it's somewhat trivial to create some sensing mechanics for the jumping: you could check the vertical velocity and see if it's slowing down, meaning the character is most likely reaching the highest point of the jump. You can use this knowledge to do decisions. Another thing is to sense the ground in advance with some raycast. This way you could trigger animation state change in Animator so that the character landing animation is timed how you want it. I've used that kind of approach and it works quite ok.
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    The 3D Game Kit uses a Blend Tree based on your vertical speed instead of separate animations and achieves a decent result for it. My Animancer plugin (link in my signature) has an example that explains how they implemented it (in comparison to how it could be done better with Animancer).

    In your first gif it might not necessarily be predicting the landing, just using a different animation while travelling downwards.
     
  4. vitorcanonico

    vitorcanonico

    Joined:
    Oct 10, 2019
    Posts:
    4
    What I'm struggling with is how to actually time the animation. If I have a raycast that tells me how far the character is from the ground, how can I couple that with his velocity variable to change the animator's speed in such a way that it times the animation properly?

    I'm pretty sure it's both using a separate animation while traveling downwards and predicting the landing. Looking at the GIF frame by frame, it's clear the animation ends faster in shorter jumps, which indicates the character has a way of knowing how close it is to hitting the ground.