# Matching sprite walking animation with movement speed

Discussion in '2D' started by Tycellent, Sep 15, 2018.

1. ### Tycellent

Joined:
Nov 7, 2014
Posts:
27
In a 2D Side-scroller, what's the best way to match the walking animation of a sprite with the walking speed of the sprite so that it doesn't look like the "character" is sliding when it is moving. I'm currently moving my sprite via
``  new Vector2(1, 0) *  m_MoveSpeed ``
and currently the walking animation has 8 frames in it. Is it just a matter of trial and error with both the movement speed and 'Samples' (in the animation tab) to get the right look or is there an actual method to get this done correctly?

2. ### chatrat12

Joined:
Jan 21, 2015
Posts:
94
There are a few variables. Your character's stride, how many frames are in a stride, and your frame rate.

So let's say your characters stride is 0.6 meters and that 2 strides take place over 12 frames at a rate of 16 frames per second. First you divide frames per second by the frames it takes to complete the strides.

16 frames/second / 12 frames/stride = 1.33 stride/second

Our 12 frames had 2 strides so multiply by 2, we get 2.66 strides/second.

Then multiply your number of strides per seconds by your stride length, and that's how far the character would move every second/

2.66 strides/second * 0.6m = 1.6 m/second

If you want something that looks more natural, you'll have to do something that isn't linear. Your velocity is not linear when walking. You can look into backing your velocity curve into your animation via root motion.

Tycellent likes this.
3. ### Tycellent

Joined:
Nov 7, 2014
Posts:
27
Thanks for the reply! I'll definitely check that out. Would this mean that if we were use this formula and we wanted to speed up/slow down the movement speed we'd either have to speed up the animation speed or adjust the sprite sheet?

4. ### chatrat12

Joined:
Jan 21, 2015
Posts:
94
No, you wouldn't adjust the Sprite sheet, you'd increase the rate at which your frames play. If you are using the animator, you can use the scale property to speed up animation playback. Scale is one by default. So if you double the speed, set scale to 2.

Tycellent likes this.
5. ### Tycellent

Joined:
Nov 7, 2014
Posts:
27
Ahh i see. Awesome! Thanks again for your response

6. ### Tycellent

Joined:
Nov 7, 2014
Posts:
27
Just to pick your brain probably for the final time. Currently with the formula that you provided it's looking great and i'm pretty happy with the result. However, in regards to the animation clip's "Samples" rate what is the best method in determining the "Samples" rate. Is it just a matter of trial and error?

7. ### chatrat12

Joined:
Jan 21, 2015
Posts:
94
Yeah, you'll have to tweak the samples until the animation looks like it's playing back at an appropriate rate to you.

Tycellent likes this.
8. ### Tycellent

Joined:
Nov 7, 2014
Posts:
27
Ahh no problem. You're a legend, thanks for all the help!

unityunity