- Home /
 
AddTorque stops working when model is vertical
I've created code for a rigidbody spaceship and it works perfectly unless the model angles vertically (X=270 or 90). When it hits that, the model still angles very slightly but snaps back into place after I let go of the button, like it's on a very tight rubber band. When this is happening, the Transform on the model doesn't show any change in rotation. Here's my code:
 var MaxSpeed = 50;
 
 function FixedUpdate () {
     
     if(Input.GetKey(KeyCode.RightArrow) == true) {
         rigidbody.AddTorque(Vector3.up);
     } else if(Input.GetKey(KeyCode.LeftArrow) == true) {
         rigidbody.AddTorque(Vector3.down);
     }
     
     if(Input.GetKey(KeyCode.UpArrow) == true) {
         rigidbody.AddRelativeTorque(Vector3.right);
     } else if(Input.GetKey(KeyCode.DownArrow) == true) {
         rigidbody.AddRelativeTorque(Vector3.left);
     }
 
     
     if(Input.GetKey(KeyCode.LeftShift) == true) {
         rigidbody.AddRelativeForce(Vector3.forward * 10);
     }
         vel = rigidbody.velocity.magnitude;
         if (rigidbody.velocity.magnitude >= MaxSpeed) {
             vel = MaxSpeed; 
         }
         rigidbody.velocity = transform.forward * vel;
         transform.localEulerAngles.z = 0;
 }
 
               I've discovered that if I comment out transform.localEulerAngles.z = 0 then the problem goes away however I cannot see the reason for this. I need some way to constrain the z axis and doing that on the rigidbody doesn't work due to my use of AddTorque. If you know why the model freezes vertically or know of a better way to constrain the local z axis, please let me know. Thank you.
Your answer
 
             Follow this Question
Related Questions
If angle is greater than 5, do something, if less than -5, do something else [Answered] 2 Answers
Using Torque to kill Angular Velocity 2 Answers
Bike stay upright based on surface normal 1 Answer
AddRelativeTorque() Applied Incorrectly When Using a Custom Centre of Gravity 0 Answers
Why can't I assign a rotation to a rigidbody Object? 1 Answer