- Home /
Finding A Rigidbody's Rotation Speed And Direction
Hi, quick question:
speed = Vector3 .Dot (rigidbody.velocity , transform.forward )*50 ;
I have this for finding the velocity of my rigidbody, how could I do something similar to find out how fast and to which direction the body is rotating?
Many thanks :)
Magnus
Answer by MarkD · Oct 12, 2013 at 10:05 AM
Well to start it is simply to finds it speed in 'velocity'. you Don't need a Vector3 to is making stuff more complicated than needed. speed= rigidbody.velocity.magnitude; //This is more than enough, with magnitude it will spit out a //single float value and will be the same as your Vector3 //version and now to find your rotation speed you could use (note this is part of my own script and only enables to one axis at a time).
The >=0 means it is moving in the positive site of its rotation the
private var lastRot:Quaternion;
function Update(){
//check if object is rotating
if(transform.rotation.y != lastRot.y && transform.rotation.y-lastRot.y >=0){
your function here;
}
if(transform.rotation.y != lastRot.y && transform.rotation.y-lastRot.y <=0){
your function here;
}
lastRot = transform.rotation;
}
Sorry about the late reply man :P
It worked perfectly thanks!
Answer by Tomer-Barkan · Oct 12, 2013 at 10:30 AM
Just like you have rigidbody.velocity
, you also have rigidbody.angularVelocity
which gives you the "rotation velocity" (known in physics as angular velocity).
Read about it here:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-angularVelocity.html
Thanks for the response, sorry about the late reply :P
When using rigidbody.angularVelocity
on my AI, it constantly reads 0 even when turning.
Does this have something to do with me using LookRotation
to rotate the AI, ins$$anonymous$$d of an actual force?
Yep, thats it. angularVelocity like velocity is the settings used by the physics engine. If you rotate the object manually ins$$anonymous$$d of letting the physics take care of it, it will be 0.