Search Unity

Pixel perfect root motion

Discussion in '2D' started by BenderBlai, Sep 13, 2019.

  1. BenderBlai

    BenderBlai

    Joined:
    Sep 13, 2019
    Posts:
    2
    What I want to achieve has surely already been created, but I don't know if it has a name, I called it pixel perfect motion but that basically means nothing, so I'll try to explain it with words since I've neither seen an image that exemplifies it.

    So I have a regular pixel art animation of a character for a 2D game, and for example in the walking animation, I can determine the distance the character moves by counting the pixels the grounded foot translates from one frame of the animation to the next one (and then I just convert the pixels to space units accordingly), It's a pretty easy thing to do actually. Then, I want to make those translations happen ingame, but with TRANSFORMS, not TRANSLATIONS, and that would give the effect that, in the walking animation, the grounded leg would always remain in the same spot unaltered, just like in real life, for the rest of the body to move accordingly. How can I make those transforms?

    Purely with code it would be a mess, since every frame of the animation has different motions, and the Animation window doesn't seem to work since it only operates with absolute positioning on the transform property.

    If I'm not mistaking, what I want to achieve is actually called root motion, but if you google that term, only references with 3D models appear, and not pixel perfect.

    Also note that what I want is not pixel perfect camera, it goes one step further since the player would never translate. If done properly, a pixel perfect camera wouldn't be needed with this effect.
    And finally, thanks and sorry for my weird english. Hope you could understand something.
     
  2. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,243
    Well, what you're describing is simply a matter of matching the speed of your sprite animation with the motion of your character. In order for it to look like your character's planted foot is motionless relative to the ground, The rendering for your character also needs to remain motionless for the duration of the frame playing. So your character's sprite element would only move forward when the sprite animation's frame advances.

    This can be better facilitated by not relying on the transform of your sprite for your character's position in space. I recommend parenting a sprite to an empty game object. The empty would serve as your "root" character and it's transform component would be your character's true position. You could then adjust your sprite's relative position to the root in real-time in order to perform the effect you are describing.

    There's also the issue of the frame-rate of your animation. The speed of your character will likely not remain consistent, it usually varies for most characters in most games. Since your sprite needs to only shift position on frame changes, you will need to take manual control of your animation's playback. You will also likely need to speed up and slow down your animation based on the current speed of the character. Thankfully, all of this is mainly just math and testing.
     
  3. BenderBlai

    BenderBlai

    Joined:
    Sep 13, 2019
    Posts:
    2
    Okay that shuld work. I'll have to program it after all...
    Its just more laborious that it would seem