unity clamp rotation 1

unity clamp rotation

float rotationX = 0;
float rotationY = 0;
// you might also have some rotation speed variable
void Update() {
   rotationX += Input.GetAxis("Vertical") * Time.deltaTime;
   rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
   rotationY += Input.GetAxis("Horizontal" * Time.deltaTime;
   transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
 }

Here is what the above code is Doing:
1. We have two variables to store the rotation values.
2. We have a min and max rotation value for the X axis.
3. In the Update() function, we add the vertical input to the rotationX variable.
4. We clamp the rotationX variable between the min and max values.
5. We add the horizontal input to the rotationY variable.
6. We set the rotation of the transform to the rotationX and rotationY values.

Similar Posts