Constrain mouse look x-axis rotation

Hey there,

in the mouse look script it says

But where (and how) do I set those values in the script?

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float sensitivityX = 15F;
	public float sensitivityY = 15F;

	public float minimumX = -60F;
	public float maximumX = 60F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	public float offsetY = 0F;

	float rotationX = 0F;
	GameObject cmra = null;

	public float rotationY = 0F;
	
	Quaternion originalRotation;

	void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX/60*cmra.camera.fieldOfView;
			rotationY += (Input.GetAxis("Mouse Y") * sensitivityY/60*cmra.camera.fieldOfView + offsetY);

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationY = ClampAngle (rotationY, minimumY, maximumY);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
		}
		else if (axes == RotationAxes.MouseX)
		{
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationX = ClampAngle (rotationX, minimumX, maximumX);

			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			transform.localRotation = originalRotation * xQuaternion;
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY + offsetY;
			rotationY = ClampAngle (rotationY, minimumY, maximumY);

			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			transform.localRotation = originalRotation * yQuaternion;
		}
		offsetY = 0F;
	}
	
	void Start ()
	{
		cmra = GameObject.FindWithTag("MainCamera");
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
		originalRotation = transform.localRotation;
	}
	
	public static float ClampAngle (float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp (angle, min, max);
	}
}

Thanks very much in advance,

Carnivore

I’m going to take a stab at:

public float minimumX = -60F;
public float maximumX = 60F;

public float minimumY = -60F;
public float maximumY = 60F;

I allready tried that (as you can see on the numbers, in the original script the public float minimumX and maximumX were -360 and 360). On the y-axis the camera rotation is constrained, but I’m still able to look around in 360 degrees.

Any other idea?

Thanks and cheers,

Carnivore

Maybe you can reduce the angles in “ClampAngle” (or have a “ClampAngleX” version just when calculating horizontal changes). That might work.

As far as I understand the original script that comes with Unity doesn’t constrain the x rotation at all.
Here’s a script that constrains the x rotation too.

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float sensitivityX = 15F;
	public float sensitivityY = 15F;

	public float minimumX = -90F;
	public float maximumX = 90F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	float rotationY = 0F;
    float rotationX = 0F;

	void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{
            // Original code doesn't constrain x rotation
            //rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

            // Constrain the x rotation the same way as the y rotation
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);

			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);

            transform.localEulerAngles = new Vector3(0, rotationX, 0);
        }
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
}