- Home /
can anyone help me stop a rigidbody from rotating around its z axis
I have been working on creating a script that rotates a object using the mouse along it's y and x axis but i want it's z axis to return to zero after turning, does any one have any suggestions.
this is what i have so far.
#pragma strict
var xspeed = 4.0;
var yspeed = 4.0;
//var zspeed = 4.0;
function FixedUpdate ()
{
//Ignore this stuff it was leftovers from my earlier attempts//
// var z : float;
// if(transform.eulerAngles.z > 0.5f)
// {
// z = -zspeed;
// }
// if(transform.eulerAngles.z < -0.5f)
// {
// z = zspeed;
// }
var x = xspeed * Input.GetAxis("Mouse Y");
var y = yspeed * Input.GetAxis("Mouse X");
rigidbody.AddTorque(x, y, z);
// Debug.Log(z);
}
Answer by gharbill · May 06, 2013 at 01:21 AM
A better option is to use Transform.Rotate(x,y,z)
since using torque does not give you instant rotation as you have to wait for angular velocity to increase.
Your code should be like this:
var x = xspeed * Input.GetAxis("Mouse Y");
var y = yspeed * Input.GetAxis("Mouse X");
transform.Rotate(xspeed,yspeed,0);
If you use a rigidbody u can constrain any axis rotation using inspector if you dont want any unwanted behaviour
Hope that helps...
The problem i was having with this fix before was that when i was testing constant forces it moved wrong, traveling in a straight line even when i turned, Since i am using this in a a flight game i was planning on handling speed with the built in physics. i was wondering if there was a better way of changing velocity?
then you can use Rigidbody.angularVelocity
to change angular velocity using physics. it would be like this :
Rigidbody.angularVelocity = Vector3(xspeed,yspeed,0);
and remember that the axis that you change in code is not always what you expect to be changed in editor depending on world or local coordination being used. so just play with x,y,z to find out which one should be zero.