3D space movement: character stuck at edge in free space
I am trying to create a third person space ship movement, where spaceship is always in center of user view. Mouse movement controls the direction spaceship faces, and W/S keys for throttle. I have the spaceship gameObject set as a parent of the camera.
The problem is in the direction part: When I try to rotate around X axis, it doesn't make a full circle! Starting from 0 for transform.localEulerAngles.x, it gets stuck at 270 or 90, depending on direction I am going. This doesn't happen when rotating around y axis, it rotates continuously forever. My script is symmetric w.r.t x and y axes, not sure why this is happening.
The whole script is pasted below. Thanks in advance!
 using UnityEngine;
 public class RotationByMouse : MonoBehaviour
 {
     private float speedX;
     private float speedY;
     public float accelarationHorizontal;
     public float accelarationVertical;
     public float deceleration;
     public float maxSpeed;
     private const float maxMouseIdleTime = 0.1f;
     private const float epsilon = 10E-4F;
     void Update()
     {
         float mouseX = Input.GetAxis("Mouse X");
         float mouseY = Input.GetAxis("Mouse Y");
         if (Mathf.Abs(mouseX) > epsilon || Mathf.Abs(mouseY) > epsilon)
         {
             float accelerationX = accelarationHorizontal * mouseX;
             float accelarationY = accelarationVertical * mouseY;
             speedX = speedX + Time.deltaTime * accelerationX;
             speedY = speedY + Time.deltaTime * accelarationY;
             speedX = Mathf.Clamp(speedX, -1.0f * maxSpeed, maxSpeed);
             speedY = Mathf.Clamp(speedY, -1.0f * maxSpeed, maxSpeed);
         }
         else
         {
             // If no mouse input, decelerate to stop
             if (speedX < 0)
             {
                 speedX = Mathf.Clamp(speedX + Time.deltaTime * deceleration, -1.0f * maxSpeed, 0);
             }
             else
             {
                 speedX = Mathf.Clamp(speedX - Time.deltaTime * deceleration, 0, 1.0f * maxSpeed);
             }
             if (speedY < 0)
             {
                 speedY = Mathf.Clamp(speedY + Time.deltaTime * deceleration, -1.0f * maxSpeed, 0);
             }
             else
             {
                 speedY = Mathf.Clamp(speedY - Time.deltaTime * deceleration, 0, maxSpeed);
             }
         }
         float rotationX = transform.localEulerAngles.y + speedX * Time.deltaTime;
         float rotationY = transform.localEulerAngles.x + speedY * Time.deltaTime;
         transform.localEulerAngles = new Vector3(rotationY, rotationX, transform.localEulerAngles.z);
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Moves itself. 1 Answer
Player keeps spinning after collision 0 Answers
My 2d movement script isn't working 0 Answers
Input.GetAxisRaw always returns -0.6 1 Answer
Make the Player unable to move to opposite direction 1 Answer