# Preserving Velocity during rotations

 0 Essentially, what I want is when the Rigidbody turns (performed by rigidbody.AddRelativeTorque) it will correct the velocity so that the velocity is relative to the transform.forward. So if I have a Global Velocity of Vector3(0.0f, 0.0f, 100.0f) with a transform.forward of Vector3(0.0f, 0.0f, 1.0f) which then changes to Vector3(1.0f, 0.0f, 0.0f) which would mean the Velocity would be updated to be Vector3(100.0f, 0.0f, 0.0f). I've been using this to do it so far, simply using the global transform.forward * Velocity.magnitude: ``````rigidbody.velocity = new Vector3(transform.forward.x * rigidbody.velocity.magnitude, transform.forward.y * rigidbody.velocity.magnitude, transform.forward.z * rigidbody.velocity.magnitude); `````` This works well except if there is a negative velocity involved, for instance a Velocity of Vector3(0.0f, 0.0f, -100.0f) when transformed with this process will loose the signed value as the magnitude is absolute so instead of (using the same case as before forward = Vector3(1.0f, 0.0f, 0.0f)) the Velocity being Vector3(-100.f, 0.0f, 0.0f) it's Vector3(100.0f, 0.0f, 0.0f). There's probably a simple fix for this but I just can't see it today... more ▼ asked Nov 11 '10 at 04:32 AM Some Programmer Type Person 1 ● 1 ● 1 ● 1 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 You probably want to have an if, then - ``````if ( Vector3.Angle( rigidbody.velocity, transform.forward ) > 90 ) { // if thing moving backwards // multiply by negative rb.vel.magnitude } else { // use current code `````` more ▼ answered Nov 11 '10 at 05:40 AM Loius 10.7k ● 1 ● 11 ● 41 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2173
x1797
x318

asked: Nov 11 '10 at 04:32 AM

Seen: 859 times

Last Updated: Nov 11 '10 at 04:32 AM