- Home /
C# - local angularVelocity
Hello, i've been asking this question few times already. I wonder if it's that hard or no one answers for some other reason.
I need to detect objects rotation in local axis. Not to make it rotate in local axis but to detect its rotation in local axis.
My code:
if (Spaceship.rigidbody.angularVelocity.z > 0)
It does what i need but only in respect to worlds axis.
Thanks for the help.
Come on guys. Don't you know the solution to this see$$anonymous$$gly simple problem? Where are you pros?
$$anonymous$$aybe no one is answering in this topic because of :
Hello, i've been asking this question few times already.
You are mixing up the rotation of the object, meaning the current quaternion applied to it, with the velocity (how fast it turns). Which one do you really need ?
Answer by robertbu · Mar 24, 2014 at 03:06 PM
This should work:
var localAngularVelocity = transform.InverseTransformDirection(Spaceship.rigidbody.angularVelocity);
Note that Rigidbody.angularVelocity is in radians. Shouldn't matter if all you are doing is testing against 0.0.
Could you please incorporate that to my code? I have no idea where that should go.
I've tried as follows:
var localAngularVelocity = transform.InverseTransformDirection(rigidbody.angularVelocity); if (-localAngularVelocity.z > 0) rigidbody.AddForce (-transform.up * 500);
And the result is still dependant on the direction that "Spaceship" is facing. Thanks for your answer tho.
Your code looks fine, so I believe it gives you what you asked for, but perhaps not what you need. In particular, there is no guarantee that the rotation you want will be represented in the way you expect. Can you describe what you are trying to achieve?
I will try.
It is the base part of my flight control system for the spaceship. The flight control system should kill any rotation by detecting the rotation and firing appropriate thrusters.
Yes i could have done it the simple way, i could have just made the spaceship rotate by itself and have angular drag but my project is spaceship simulator.
So again, basically, i want the code to check if there is any angular speed, on which axis and to which direction. It perfectly does that but not the last part - it ignores local (spaceship) axis and uses world axis so if the spaceship is facing another way, the rotation is accelerated ins$$anonymous$$d of stopped.
I hope i've made it clear now.
Thanks for your help man.
You made it clear(er), but I cannot see how 'AddForce()' on the same object that you are using for calculating the angular velocity will impact the angular velocity at all. That is the force will be applied to the center of mass which is also the center about which this object is rotating. You will need an AddForceAtPosition() or some other way to offset the force from the center of mass. And if you do that, I don't believe you want the local angular velocity.
The "Spaceship" is a separate object from that to which AddForce line applies. The object is another rigidbody (a thruster) on the side of the spaceship so it stops the rotation with applied force to it.
Your answer
Follow this Question
Related Questions
Child versus Parent rotations 3 Answers
how to apply overall rotation to collection of rotated objects 0 Answers
Maintain world rotation on parenting to another object 0 Answers
Get the Inspector's rotation of an object with code 0 Answers
Importing a armature from blender to unity ruins the models rotations -1 Answers