Calculate step interval


I am making a custom first person controller and I want my footstep interval to change according to my player velocity. I have an idea where I put a step length value and then calculate the path that player traveled and every time it’s greater or equal than the step length, play footstep sound and reset the path traveled to 0.

How can I calculate the path that the player traveled?

Thanks in advance!

I’m pretty sure that basing the footstep sound on how long the player has traveled is not what you want. That would mean that if you walked just a little bit, stopped, walked just a little bit again, stopped, and so on, you’d get the step sound just some of the times. That would sound weird.

That being said, it’s pretty easy to do something based on cumulative movement:

private Vector3 lastPos;
private float distMoved;

void Update() {
    distMoved += (lastPos - transform.position).magnitude;
    lastPos = transform.position;

    if(distMoved > someValue) {
         // play sound

         distMoved = 0;

I would rather count time while you’re moving forward, and reset the counter when you stop moving. Then it’ll be consistent how long after you’ve started moving you get the footstep sound.