- Home /
 
Need help with this script
Hi, i'm new to scripting, i have the following code and need some help clamping the Z rotation because its not working this way as sometimes the object still spins 360º. I'm trying to control the player using just the horizontal and vertical arrows to move up/down sideways(tilting at the same time) and it basically works but for some reason after a while the player won't rotate on Y i have t let go of the key and press again.
 function FixedUpdate () {
     if (Input.GetKey(KeyCode.W))
     speed = speed +1 * acceleration;
     if (Input.GetKey(KeyCode.S))
     speed = speed -1 * acceleration;
     if (speed >= maxSpeed)
     speed = maxSpeed;
     if (speed <= minSpeed)
     speed = minSpeed;
     
     rigidbody.AddRelativeTorque (0, Input.GetAxis("Horizontal") * spin * Time.deltaTime, 0);
 
     if (Input.GetAxis("Horizontal") > .1 && transform.rotation.z > -0.1) {
     rigidbody.AddRelativeTorque (0, 0, -spin * Time.deltaTime);
     }
     
     if (Input.GetAxis("Horizontal") < -.1 && transform.rotation.z < 0.1) {
     rigidbody.AddRelativeTorque (0, 0, spin * Time.deltaTime);
     }
     
     
     if (Input.GetAxis("Vertical") > .1 && transform.rotation.x > -0.3) {
         rigidbody.AddRelativeTorque (-torque * Time.deltaTime, 0, 0);
     }
     if (Input.GetAxis("Vertical") < -.1 && transform.rotation.x < 0.3) {
         rigidbody.AddRelativeTorque (torque * Time.deltaTime, 0, 0);
     }
     rigidbody.AddRelativeForce(0, 0, speed * Time.deltaTime);
 
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Horizontal Drag, no Vertical Drag 1 Answer
How to set velocity of Rigidbody without changing gravity? 1 Answer
Gravitational pull without losing speed 1 Answer
How to keep gravity when adding velocity to Rigidbody 3 Answers
G Force Acceleration 0 Answers