- Home /
Adjust movement while rotating rigidbody
I try to design something like a top down racer. I used the rigidbody component for the car to move it with addRelativeForce and transform.Rotation (for precise control) and it works very well while moving forward. But if you dont press up, so no forward RelativeForce is added anymore, you can't change the car's direction but only rotate itself, while it's moving still further the "old" direction. If you add a forward force again, it'll move into that new direction.
So as the title says im looking for an option to adjust the force or the movement if you rotate the rigidbody to change the direction without acceleration.
Answer by robertbu · Jul 13, 2014 at 03:16 PM
Probably the best choice would be to up your drag. This will cause the old velocity to decay faster and therefore your car will go in the new direction faster. You will likely have to increase the amount of force applied each frame to compensate for the upped drag.
If you really want to make an immediate turn, you can do something like this in FixedUpdate():
var mag = rigidbody.velocity.magnitude;
rigidbody.velocity = transform.forward * mag;
Thanks, had to adjust a few things to use it in my project (used local vector ins$$anonymous$$d of global) and to be able to drive back the same way but you gave me the right hint. Here the code if somebody has the same problem:
if (transform.InverseTransformDirection(rigidbody.velocity).z < 0) {
float mag=rigidbody.velocity.magnitude;
rigidbody.velocity = -transform.forward * mag;
} else if (transform.InverseTransformDirection(rigidbody.velocity).z > 0) {
float mag = rigidbody.velocity.magnitude;
rigidbody.velocity = transform.forward * mag;
}