- Home /
What is the velocity on a given axis of a rigidbody?
I'm looking to find out what the velocity of a rigidbody is on a specified local axis.
I'm building a simulation/shooter based on scifi airship warfare, which includes both planes and floating anti-gravity airships. For the planes, I'd like to get the speed along the z-axis for a simplified calculation of lift. In aerial geek-talk, I'm basically interested in Indicated Airspeed.
For the airships, it will be used to aid calculating collision damage, hopefully in conjunction with a normal-line between the colliding parts. The logic is that fast-front hitting damage will do more than a gentle glancing bump.
Can anyone suggest the most efficient way to do this? I'm really looking for the closest actual equivalent to "rigidbody.GetLocalVelocity(z);"
Answer by Peter G · May 21, 2010 at 01:33 AM
You could do what the Car tutorial does. To find the local velocity it goes:
function Update () {
//find the local velocity of the rigidbody.
var relative : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
}
or when you use OnCollisionEnter(col : Collision), consider using Collision.RelativeVelocity.
Aha, I shall try this at the first opportunity, I think it's just what I'm looking for. And Collision.RelativeVelocity was not something I was aware of either; thanks for that! That will make the airship part of the problem vastly simpler.
Answer by qJake · May 20, 2010 at 11:49 PM
I'm confused. Why can't you just use Rigidbody.velocity.x/y/z
, or GetLocalVelocity()
, as you said?
You could also write your own speed calculator, just save the x/y/z position from the previous Update and then subtract it from the current one, which gives you the speed. Something along the lines of:
float lastPos; float speed;
void FixedUpdate() { speed = Mathf.Abs(lastPos - transform.position.z); // or position.x/y if you need that lastPos = transform.position.z; // or x/y. }
rigidbody doesn't have a GetLocalVelocity method, but if it did it would be exactly what I'm looking for. Apologies for confusion. -- rigidbody.velocity.x/y/z is in world space. I'm looking to know the objects speed relative to it's own axis.
What do you mean relative to its own axis? A game object only has local coordinates if it has a parent object...?
All objects have their own co-ordinate space, this is effectively what a Transform is. The 'local' coordinates for positioning a child object are in fact it's position in the parents coordinate space. And the reason I need this forward speed is because airflow only provides lift when passing over the wings in the direction of the centre line (z axis of the aircraft). The same amount of movement in a vertical direction (for example) would not produce lift.
Right, but you still need to get the global velocity of the entire plane, the wings don't move independently of the plane, they're attached, so even though you need the Z-velocity, it needs to be the global velocity. I'm pretty sure Rigidbody.velocity.z is what you need.
I did try to use Rigidbody.velocity.z, before I realised it was in global terms. Global z is essentially your movement 'north/south' right? (to avoid confusion of what I mean here) If you were to head 'east', your global z coord won't change. So your speed readout would be zero. Zero in the north/south direction, yes, but this is not what I needed. I needed the speed along the forward/back axis of the aircraft, not the world, which is what the above car tutorial snippet gives =)