# Why doesn't my rigidbody velocity limiter script work properly?

I’ve been trying to limit the velocity for 20 mins now without it being strange.
So, yes it does limit the velocity the max velocity is 15 but when my character rotates and I press W to go forward. Both x velocity and z velocity go to 15 or -15. Even when I try to go as straight as possible to go forward only in the x axis it still affects the z velocity and makes it go to 15. I’ve checked that the maxVelocity[0] is -15 and maxVelocity[1] is 15 but it still doesn’t work. I just don’t know what to do. Should I just give up and let the player character roam around with the speed of a car? If someone can explain what is wrong with my script I’d really appreciate it, because I can’t see anything wrong with it and it would be a good learning experience for me to learn how to make a velocity limiter.

``````    void VelocityLimit()
{
rb.velocity = new Vector3(Mathf.Clamp(rb.velocity.x, maxVelocity[0], maxVelocity[1]), rb.velocity.y, Mathf.Clamp(rb.velocity.z, maxVelocity[0], maxVelocity[1]));
}
``````

It’s hard to know for certain why your character would automatically jump straight to the maximum thresholds based solely on your script example (Rather, it very well may be another part of the script causing that), but there’s still a straightforward solution to the main question.

Start by separating the X- and Z- axes from the velocity, normalize/scale if needed, then put the Y-axis back into place:

``````// Get the X/Z-axes from velocity
Vector3 clampedVelocity = new Vector3(rb.velocity.x, 0.0f, rb.velocity.z);
// Check the magnitude of the axes
if(clampedVelocity.sqrMagnitude > maxVelocity * maxVelocity)
{
// If greater than the limit, set them to that limit
clampedVelocity = clampedVelocity.normalized * maxVelocity;
}