- Home /
gameobject still rotating over z axis
Hi guys, sorry for my english. i'm realy stack. i'm new with unity and physics. in my project i want to rotate gameobject over z axis when it pass 0.1 angle. The problem is that the gameobject still rotating even after reseting it rotation. One more question, i don't understand why i can't rotated the gameobject after reseting rotation (this code work one time before reseting rotation) and this is my code.
void FixedUpdate()
{
if (this.gameObject.transform.rotation.z > 0.1 || this.gameObject.transform.rotation.z < -0.1) {
if (this.GetComponent<Rigidbody> ().constraints == RigidbodyConstraints.None) {
this.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationZ;
}
}
if (myInput > 0.5) {
Debug.Log("Rotate left");
rotateLeft ();
} else if (myInput < -0.5) {
Debug.Log ("Rotate right");
rotateRight ();
}
else {
if (this.gameObject.GetComponent<Rigidbody> ().constraints != RigidbodyConstraints.None) {
if(this.gameObject.transform.rotation.z == 0)
this.gameObject.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.None;
if (this.gameObject.transform.rotation.z != 0)
this.gameObject.transform.rotation = new Quaternion (transform.rotation.x, transform.rotation.y, 0, transform.rotation.w);
}
}
}
void rotateLeft()
{
if (this.gameObject.transform.rotation.z > /*amount of degrees*/ 0.4f)
return;
//rotate more
transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(transform.rotation.x,transform.rotation.y,0.4f,transform.rotation.w), Time.deltaTime*50f);
Debug.Log("0.4f atteind");
}
void rotateRight()
{
if (this.gameObject.transform.rotation.z < -0.4f) {
return;
}
//rotate more
transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(transform.rotation.x,transform.rotation.y,-0.4f,transform.rotation.w), Time.deltaTime*50f);
Debug.Log("-0.4f atteind");
}
is the problem of rigidbody or what? and for the best parctice do i use transform.rotate and transform.rotation or rotate the gameobject with rigidbody. thanks
The script is working fine, there are no extra rotation. Are you sure there are no other script disturbing ?
hi. my gameobject is a car and i'm using edy's vehicle for physics. i don't know if there are other script disturbing, as i said i'm new with unity and physics
Well, if you use a pluggin and try to act on a GameObject tied to a script, there may be some conflicts.
Be sure to look Edy's vehicle documentation to gather all the necessary informations for the purpose of your needs. Then, go step-by-step in order to catch where your script miss.