Limit rigidbody forward, backward and sideways velocity separately.

Hi, I need to limit velocity of my rigidbody but I need separate limiting for forward, backward and sideways moving. I have figured out how to limit velocity of all directions at same time but can’t figure out how to limit all directions separately.

I want my character to move slower in sideways and backward than forward.

Thanks in advance,
Paul

Velocity is world velocity, so the first this is to convert it to a local velocity:

var localVelocity = transform.InverseTransformDirection(rigidbody.velocity);

Now you can clamp the axes independently:

localVelocity.x = Mathf.Clamp(localVelocity.x, -3.25, 3.25);

Then convert it back to a world direction and assign it:

rigidbody.velocity = transform.TransformDirection(localVelocity);