Search Unity

2D Sprite Animation in 4 Directions - But movement can also go Diagonal

Discussion in 'Animation' started by Chyro, Jun 21, 2019.

  1. Chyro

    Chyro

    Joined:
    Nov 5, 2018
    Posts:
    8
    Hey there.

    Seeking advice for a minor detail that I can't get quite right.
    I successfully made an animation controller for my player character that animates it moving in all four directions, as well as turning to the respective idle-animation for each direction when standing still.

    Now my character is also able to move diagonally, so I'm trying to make it 'feel right' at which time the animation direction changes. The movement is based on horizontal and vertical axes, so the animations are also based on those values.

    At first I made the condition that it can only change to the facing-right animation if the vertical axes is greater than -0.5 and lower than 0.5 and horizontal greater than 0 (so basically if you're not too strongly pressing up or down on controller, you turn right when going even a little to the right). And these same conditions for each direction. This felt really good and fluid, but had the problem that when moving exactly diagonal to the point that the horizontal and vertical axes are equal, and both are above -0.5 and under 0.5, the sprite would rapidly flicker between the two directional animations.

    I wanted to fix this, so I tried (again for moving-right as example) the transition conditions as vertical must be 0 (greater than -0.01, lower than 0.01) and horizontal greater than 0. This makes the flickering impossible since only one directional animation can ever play at one time. But.. this feels rather stiff when moving around, cause you're stuck in the directional animation you're moving in at first until you stop moving on that axis and are moving in the other direction instead. (like if you start moving up, the up-walk animation keeps playing no matter how strongly you move to the right until you stop moving up at all and move only to the right)

    So.. was wondering if anyone else has dealt with this problem before and maybe knows a simple solution. Is it possible with just tweaking the transition conditions? Or would I possibly have to check and manipulate the horizontal / vertical axis values in code before passing them to the animator parameter?

    I guess the desired behavior would be that the first directional animation plays (for example, moving up) and when moving over diagonal to the right, as soon as the vertical axes is 'stronger' than the horizontal, it should change to the other animation. So vertical 0.5, horizontal 0.5 would still play the moving-up animation (if that's the animation that played before getting to that point) but as soon as horizontal is greater than 0.5 while vertical stays 0.5 or lower, it should swap to the moving-right animation. But in the transition condition I can't seem to make conditions that are more complex or compare one parameter to another.
     
    Last edited: Jun 21, 2019
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    My Animancer plugin (link in my signature) has some example scenes that show how this sort of thing can be easily implemented.