Rotating 3d spaceship

Hello guys, I have a problem.
I have a spaceship which I would like to rotate around y-axis when left/right has been pressed. And I would like to clamp the rotation to like 40 degrees left and right. Then when you lift the key I would like to smoothly return the rotation to 0.
I understand that you can use one of following things to rotate plane:

  1. shipTransform.Rotate(Vector3.up * 30f * Time.deltaTime); for left | shipTransform.Rotate(Vector3.up * -30f * Time.deltaTime); for right
  • how to clamp the value of this rotation so it only rotates until shipTransform.rotation.y = 40 || -40?
  1. Vector3 currentRotation = shipTransform.localRotation.eulerAngles;
    Here I can change the currentRotation.y but how to clamp the rotation of it again?
    shipTransform.localRotation = Quaternion.Euler (currentRotation);
    currentRotation.y = Mathf.Lerp (currentRotation.y, 0f, 0.01f);
    I somehow managed to clamp the rotation when you go to the left and then when you stop pressing it reduces the rotation to 0. But in that case when you go to right it spins the spaceship for 360 before rotation comes to 0?why is that?
    I think its clear what I am having problems with. If not I am happy to supply more info. Thx guys!
1 Like

Privacy & Terms