- Home /
Movement Physics: Max Speed and Momentun
Argh! I hate to waste my first post asking a question instead of helping someone else... sigh
Anyway, here's the rundown of what I have in place: I have a rate of acceleration and a maximum speed. I'm scaling my acceleration based on the relative point between my current velocity's magnitude and the maximum speed. Then, I'm also scaling my rate of acceleration based on the dot product of my directional input (normalized vector from keyboard) and velocity to allow movement in other directions when I reach my maximum speed.
var lateralMovementScale: float = Vector3.Dot(movementDirection, rigidbody.velocity);
var finalMovementSpeed: float = Mathf.Lerp(0, accelerationRate, (movementSpeedMax - lateralMovementScale) / movementSpeedMax);
finalMovementSpeed = Mathf.Clamp(finalMovementSpeed, 0, accelerationRate);
rigidbody.AddForce(movementDirection * finalMovementSpeed, ForceMode.Acceleration);
With those particular lines as my basis right now, my movement never puts me above my max speed (when I don't give myself acceleration of ~1000 to reach a maximum speed of 3, anyway). That's good, but the problem arises from the fact that my math was slightly off. If I'm moving at my maximum speed and change direction, I can accelerate beyond my maximum speed up to roughly (2*maxspeed) squared, since the acceleration is purely additive at that point. I can't just limit my maximum velocity to be my maximum speed because that influences falling and getting shoved or otherwise building momentum. Ideally, if I'm already exceeding my maximum speed, I won't instantly drop down to normal in order to change direction to a side.
The way things are now, it's more effective to move by snaking back and forth to maintain a higher movement speed than traveling in a straight line, but that's not at all what I'm attempting to achieve.
How should I approach moderating speed to dampen that momentum, or am I simply taking the wrong approach to this altogether?