I need to limit the rotation of my camera.

Hello,

I am creating a Third Person Controller script with a movable camera, and have issues with being able to rotate 360 degrees vertically. I want my character to keep his head, so I obviously want this error resolved. Here is (part of) my script:

float hori = horizontalSpeed * Input.GetAxis("Mouse X");
float vert = verticalSpeed * Input.GetAxis("Mouse Y") * -1;
rbody.transform.Rotate(0, hori, 0);
myCam.transform.Rotate(vert, 0, 0);

I have tried to clamp the values using Mathf.Clamp and either I’m doing something wrong, or they aren’t what I want… When I used it, the camera looped back around to the maximum value when it went past the minimum…

Thank you in advance!

You should clamp it and rotate it in function LateUpdate.

Here is some Code:

void Update () {
		 hori += horizontalSpeed * Input.GetAxis("Mouse X");
		 vert += verticalSpeed * Input.GetAxis("Mouse Y") * -1;
	
	}

	void LateUpdate(){
		hori = Mathf.Clamp(hori,0,180f);
		print(hori);
		rbody.transform.rotation = Quaternion.Euler(0, hori, 0);
		myCam.transform.rotation = Quaternion.Euler(vert,0,0);
	}

You can tweak these values as your liking. here 0 in minimum and 180 is maximum.