# How might I rotate rigidbody momentum?

Hi,

I currently have a rigidbody spaceship that is controlled by the player. I have assumed that the spaceship should be a non-kinematic rigidbody because I want it to bounce and rotate during collisions. Please let me know if this is wrong.

The player can use the up and down arrow keys to give the rigidbody momentum (the keys enable and disable thrust). This acts like acceleration, by calling rigidbody.AddRelativeForce . This was exactly what I wanted.

I then went and made it so the player could turn the rigidbody, by implementing rigidbody.AddRelativeTorque . This did not achieve the desired effect, as the directional vector already in place caused the rigidbody to drift.

My question is: how do I rotate this rigidbody such that momentum rotates with the object? More precisely, is there a function that will do it for me, or should I use the math component to manually rotate the force vector on the rigidbody?

Thanks,

Andrew

EDIT:

Special thanks to Brian Kehrer and Horsman for putting me on the right track. I slightly modified their solution, by multiplying the original vector magnitude by the directional vector, rather than the transform vector, which yielded all sorts of crazy results.

``````rigidbody.velocity = Vector3.forward * rigidbody.velocity.magnitude;

``````

Thanks for the help!

It is not physically acurate to do this, of course, but you can acheive it easily by rotating the current velocity of the ship.

````
Vector3 velocity = rigidbody.velocity;
rigidbody.velocity = Vector3.zero;
rigidbody.velocity = transform.forward * velocity.magnitude;
```
`

It's not physical, but you could just rotate the velocity vector and reapply it. This typically results in more intuitive, better feeling controls.

EDIT The easiest way to do this is maintain a transform as the current intended velocity vector. Rotate the transform when the player rotates, and then call Transform.forward.

Multiply that by the Rigidbody.velocity.magnitude and reassign that to Rigidbody.velocity.

I wanted to model a bird flying.
I am using this in the mouse controller:

``````transform.Rotate(Vector3.up, x * xSpeed);
rigidBody.velocity = Quaternion.Euler(0, x*xSpeed, 0) * rigidBody.velocity;
``````

where x is

``````Input.GetAxis("Mouse X");
``````

In the WASD script I use:

`````` m_Rigidbody.AddForce(f * 10.0f * Camera.main.transform.forward + s * 10.0f * Camera.main.transform.right);
``````

f is forward button and s is strafe button press

``````    int f = Input.GetKey(KeyCode.W) ? 1 : Input.GetKey(KeyCode.S) ? -1 : 0;
int s = Input.GetKey(KeyCode.D) ? 1 : Input.GetKey(KeyCode.A) ? -1 : 0;
``````

I actually solved this by getting the change in the angle I’m trying to rotate on.

For reference, rb is rigidbody, pc is my camera script. prevRot is the rotation before the mouse moved.

``````rb.velocity = Quaternion.Euler(0, transform.eulerAngles.y - pc.prevRot, 0) * rb.velocity;
``````