- Home /
 
Help with rotation.
Hi. i'm currently making a game where my ship moves in 3d space. I've hit a problem where the ship rolls over if you spin the ship on it's Z axis over 90 degrees, so I want to stop it from rotating when it reaches about 65 degrees in either direction. I've tried alot of this, but haven't figured it out so far.
 public void Rotate() // boomshackalacka
     {
         float rStickY = Input.GetAxis("HorizontalRotationP1");
         float rStickX = Input.GetAxis("VerticalRotationP1");
 
         if (Input.GetAxis("HorizontalRotationP1") < 0.2)
         {
             transform.Rotate(new Vector3(0, rStickY, 0) * RotationSpeed * Time.deltaTime);
             _model.Rotate(new Vector3(0, 0, -rStickY) * RotationSpeed * Time.deltaTime);
         }
         if (Input.GetAxis("HorizontalRotationP1") > 0.2)
         {
             transform.Rotate(new Vector3(0, rStickY, 0) * RotationSpeed * Time.deltaTime);
             _model.Rotate(new Vector3(0, 0, -rStickY) * RotationSpeed * Time.deltaTime);  
         }
         if (Input.GetAxis("VerticalRotationP1") < 0.2)
         {
             transform.Rotate(new Vector3(rStickX, 0, 0) * RotationSpeed * Time.deltaTime);
         }
         if (Input.GetAxis("VerticalRotationP1") > 0.2)
         {
             transform.Rotate(new Vector3(rStickX, 0, 0) * RotationSpeed * Time.deltaTime);
         }
         if ((Input.GetAxis("VerticalRotationP1") == 0 && Input.GetAxis("HorizontalRotationP1") == 0) && _shotTimer > 0.3)
         {
             transform.rotation = Quaternion.Lerp(_playerBody.rotation, _originalRotation, Time.deltaTime * 10f); //originalRotation får den att kolla rakt fram dirr, vill bryta ut någon axel.
             _model.rotation = Quaternion.Lerp(_model.rotation, _originalRotation, Time.deltaTime * 10f);
         }
     }
 
               All help is greatly appreciated!
               Comment
              
 
               
              Answer by ExtinctSpecie · Mar 16, 2017 at 08:16 AM
     so i tried this out and as i understood the rotation function functions like a sine function with a 4*pi period time and a peak of 1 
     so the values move from  0 -> 1 , 1 - > 0  , 0 -> -1 , -1 - > 0 and then the same again 
     in your case you want to stop at about 0.5 which is about 60 degrees
         float zAx = 0;
         // Update is called once per frame
         void Update ()
         {
     
             zAx = -10 * Time.deltaTime;
             if (transform.rotation.z > -0.5f && transform.rotation.z < 0.5f) 
             {
                 //continue rotating
                 transform.Rotate(0f, 0f, zAx);
             } 
             else 
             {
                 //will stop rotate
             }
     
                 
         }
     
     i apologize for my english 
     
 
 
              Your answer
 
             Follow this Question
Related Questions
BoxCast to tell if player is grounded 0 Answers
Clear gaps/cuts in trail renderer 0 Answers
What is Matrix4x4? 2 Answers
How do i code a Mesh collider ? 1 Answer
Hello i am having some trouble converting a JS to a C# script. 1 Answer