# How do I obtain the local velocity of a Rigid body on one axis?

 0 I'm creating a simple driving game were the player controlled vehicle is a single box-like rigid body. I am accelerating the vehicle with rigid.body.AddRelativeForce and I'm steering the object with rigid.body.MoveRotation. I really need to be able to obtain the relative velocity along the vehicles local X axis (forwards and backwards) to be able to script rules and logic from, and right now all I seem able to do is get either the world velocity (rigidbody.velocity) or all-axis-combined velocity (rigidbody.velocity.magnitude) but neither of these are accurate enough for what I want to do. Can anyone offer any hints here? Thanks. more ▼ asked Nov 02 '10 at 01:19 PM Alkan 32 ● 10 ● 10 ● 12 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Try (untested): ``````float xVel = transform.InverseTransformDirection(rigidbody.velocity).x; `````` more ▼ answered Nov 02 '10 at 03:59 PM Jesse Anders 7.3k ● 7 ● 17 ● 48 Just tested it, it works -- I've been doing this manually all this time. Thanks! Nov 02 '10 at 04:08 PM VS48 That does exactly the same as the code I posted... (which works btw, just tested) Nov 02 '10 at 04:10 PM Atnas1010 I don't seem to be getting the same result Nov 02 '10 at 04:30 PM VS48 Hmm, I'm looking into that right now Nov 02 '10 at 04:38 PM Atnas1010 Hehehe this should work: Quaternion.FromToRotation(transform.forward, Vector3.forward) * rigidbody.velocity); It was correct, just I think one of the axes was inverted Nov 02 '10 at 04:50 PM VS48 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Is this what you are looking for? (transform.rotation * rigidbody.velocity).x more ▼ answered Nov 02 '10 at 02:19 PM Atnas1010 1.1k ● 6 ● 10 ● 26 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Use `rigidbody.velocity.x`. Is that what you're looking for? more ▼ answered Nov 02 '10 at 01:40 PM e.bonneville 5.7k ● 100 ● 116 ● 165 No, rigidbody.velocity.x gives me the velocity on the world x axis, not the local axis - this means it can report anything from high to low values depending on the direction I'm driving the vehicle in. Nov 02 '10 at 02:06 PM Alkan add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1788
x322
x318
x134
x3

asked: Nov 02 '10 at 01:19 PM

Seen: 4058 times

Last Updated: Feb 15 at 02:04 PM