# Limit Rotation around X Axis?

I’m completely lost, me and a group of friends who are relatively new to this are trying to create a custom mouse look script and have all but the clamping of the axis done. We have no knowledge of euler angles and quaternions and I really can’t find anything that makes sense so was hoping I could get some help here, basically we want to limit the rotation of the camera on the X axis, I understand the if and else if statements are incorrect but we couldn’t find any way to do it that makes sense, we have knowledge of Mathf.Clamp () but we can’t figure out how to use it.

``````	void Awake () {
Player = GameObject.Find ("Player");
}

void Update () {
float mouseX = Input.GetAxis ("Mouse X") * Xsensitivity;
float mouseY = Input.GetAxis ("Mouse Y") * Ysensitivity;

mouseY = -mouseY;

Cursor.lockState = CursorLockMode.Locked;

if (mouseY > 0 && mouseY < 60) {
mouseY = mouseY;
} else if (mouseY >= 60 && mouseY <= 180) {
mouseY = 60;
} else if (mouseY <= 300 && mouseY >= 180) {
mouseY = 300;
} else if (mouseY >= 300 && mouseY <= 360) {
mouseY = mouseY;
}
transform.Rotate (mouseY, 0, 0);
Player.transform.Rotate (0, mouseX, 0);
}
}
``````

Hi! So i cleaned up the code a bit (not sure why you were locking the cursor). I only left what is relative to the clamping :

``````  using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour {
public float Ysensitivity;
private float rotationY = 0f;
void Update () {
rotationY += Input.GetAxis ("Mouse Y") * Ysensitivity;

rotationY = Mathf.Clamp (rotationY, -15, 15);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
}
``````

As you can see you use Mathf.Clamp by giving it the variable you want to clamp and then the minimum and maximum values between which you want to clamp it. Then i simply assign the value to the transform’s local euler angles. I also store the current rotation in a variable at the start and modify it using the values from Input.GetAxis. Just go ahead and replace -15 and 15 with your own boundries and everything should work just fine.

Rather than trying to reinvent the wheel there is the MouseLook.cs script in the Characters Unity Standard Assets package part of the standard First Person character prefab.

I’ve taken a look and it seems to have maximum and minimum rotations on the x axis.

You could use that instead or atleast take a look at how they did it.

I also wrote my own MouseLook script and faced the same Problem you’re now facing.
You didn’t mention how your Camera Object is made so I’ll explain how I solved it.
My Controllable GameObject is in the Scene (no parents) as a Child Object to that one I have an Empty GameObject I called XRot which is only meant for the Rotation of my Camera around the X-Axis and my Camera Object self is a Child of XRot. Therefor I can set my Camera at any distance I want with it’s local Position and Rotate around some other Point (the XRot Object) around the X-Axis.
MY Script now has a Transform reference to the XRot Object and the code Looks like this:

``````        //Camera Stuff (rotating Character/looking around)
Vector3 rot = xRot.localRotation.eulerAngles;
rot.x -= Input.GetAxis(PlayerInput.MouseY) * 50 * camSpeed * Time.deltaTime;
if (rot.x > 180) rot.x -= 360;
rot.x = Mathf.Clamp(rot.x, -60, 60);
xRot.localRotation = Quaternion.Euler(rot);
transform.Rotate(0, Input.GetAxis(PlayerInput.MouseX) * 50 * _unit.moveSpeed * Time.deltaTime, 0);
``````

About Mathf.Clamp it works pretty simple as soon as you understand the the return value is the clamped value you can see how I used it in my code. here I Limit the X-Rotation from -60° to 60° relative to the initial Position 0°
I used Time.deltaTime because that code snippet is inside the Update() Event.
Hope this helps.