MouseLook

So I have edited a few small things in the original mouselook script to have it rely on a rightclick system and be restrained to a certain degree of rotation. Everything works great except that even tho I managed to get it to stay within 180 degrees left to right the center of this limit seems to be looking to the characters direct right which means i am stuck looking from the front of the character to behind it on the right side… Ive tried everything but cant seem to get the limits to make it so you can only look ahead and left and right. Here is the whole script,

using UnityEngine;
using System.Collections;

[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 = 0F;
	public float maximumX = 180F;

	public float minimumY = -60F;
	public float maximumY = 60F;
	float rotationX = 0F;
	float rotationY = 0F;

	void Update ()
	{
		if (Input.GetButton ("Fire2"))
		{
		if (axes == RotationAxes.MouseXAndY)
		{
			 rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
			
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			 Adjust360andClamp();
			transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
			transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			 Adjust360andClamp();
			transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
		}
		}
	}
	
	void Start ()
	{
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
	void Adjust360andClamp()
	{
		if (rotationX < -360)
        {
            rotationX += 360;
        }
        else if (rotationX > 360)
        {
            rotationX -= 360;
        }   
 
        if (rotationY < -360)
        {
            rotationY += 360;
        }
        else if (rotationY > 360)
        {
            rotationY -= 360;
        }
 
        rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    
 
	}
}

I can take a video of what I mean if you guys want? Just let me know.

Have you tried minimumX = -90;
maximumX = 90;

ortherwise maybe 90, 270

I tried the -90 90 and I find that when i try to look directly ahead it jumps to the far right so I cant look ahead

I tried 90 270 and that didnt work I got stuck looking behind me.

All right, what you will have to do is put a print statement in void Adjust360andClamp()

rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
print(rotationX+“”);

Take out the minimumx and max, and watch the numbers to find the ones you need.

Because 360 is directly in front of you, Mathf.Clamp may not work, or you may need two uses of it. One for greater than 0 and less than 90, one for less than 360 and greater than 270.

Well I tried and I got the values “75” and “275” which work great except… I am now looking behind rather than in front of my character… 75 for minimum and 275 for max. I tried swapping the values and I end up glitching terribly where I can only look at exactly 75 or 275 and nothing in between…

That’s because when it reaches 360, it starts at one.
The logic gets a little complicated for that.
you’ll have to do two conditions:

if(rotationX > 180  rotationX<= 360){
  if(rotationX < 275) rotationX = 275;
}
else if(rotationX>0  rotationX<180){
if(rotationX>75)rotationX = 75;
}

You’d have to put that instead of the rotationX = Mathf.Clamp
I think, or something like that. That’s assuming 75 is on the right, 275 is on the left. I didn’t watch the numbers. I’m just guessing 360 is straight in front and it’s keeping Mathf.Clamp from working.

IT WORKED!
OMG you are a LIFESAVER !!! ILOVEU! :smile: THANKYOU!