problem with camera rotation, help needed ...

hello, i’m new to unity, but i’m trying to making my way with this powerfull engine.

i’m trying to make a 3d game with a first person camera, my player doesn’t need to move, it can only move the camera, to look to the right and look to the left, i have coded that movement with the mouse using c#, but my problem is that i want to avoid the camera rotate out off the bounds that i’m using in game.

it works almost all, but when the camera arrives to the max amount to the right or left the camera stop rotating and it never rotates again.

here is the code that i’m using to let the camera rotate and stop it too

         void Update () {
		
		//camera rot
		float RotLeftRight = Input.GetAxis("Mouse X");
		
                if (transform.rotation.y>MaxRight || transform.rotation.y<MaxLeft)
		{
			 LastRotation=transform.rotation.y;
			//transform.Rotate(0,LastRotation,0);
		}
		else
		{
			transform.Rotate(0, RotLeftRight,0);
		}
		
		//movement set to 0 to let the player stays on his place
		//float ForwardSpeed = Input.GetAxis("Vertical"); //get speed from input axis
		Vector3 speed = new Vector3(0,0,0);
		
		CharacterController cc = GetComponent<CharacterController>();
		
		cc.SimpleMove(speed);
	
	}

MaxLeft and MaxRight are declared at the beginning of the class using public to let the variables to be modified from the unity editor.

i tried using LastRotation and use it as a variable to the rotate function, but when the game uses it, it keep rotate continuosly to the left side

can someone help me, point me to the right direction to fix this ?

thanks in advance for all the help.

What you’re probably looking for is Mathf.Clamp(). It clamps a value within a range.

So, something like:

transform.rotation.y = Mathf.Clamp(transform.rotation.y, MaxLeft, MaxRight);

Because, currently, it appears that you don’t reset the value to be within the range and so it is always out of range and your code never allows it to rotate again due to the structure of your if statement.

By using Mathf.Clamp, you can eliminate using the if statement altogether and just apply the rotation and then apply the clamp.

tried your way

transform.Rotate(0, RotLeftRight,0);
transform.rotation.y = Mathf.Clamp(transform.rotation.y, MaxLeft, MaxRight);

but unity told me:

cannot modify a value type return value of 'UnityEngine.Transform.rotation' consider storing in a temporary variable

after that i tried this

transform.Rotate(0, RotLeftRight,0);
FinalRotation = transform.rotation.y;
FinalRotation=Mathf.Clamp(FinalRotation, MaxLeft, MaxRight);
transform.Rotate(0, FinalRotation, 0);

but that code will let the camera always rotate to the left.

any idea ?

Hence why I said “something like”. You probably shouldn’t be dealing with the Quaternion rotation as well. And you can set the Euler Angles directly (besides, you’re probably thinking in context of the Euler Angles). So, again, something like:

transform.Rotate(0, RotationValueWhatever, 0);
float yVal = Mathf.Clamp(transform.eulerAngles.y, MaxLeft, MaxRight);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yVal, transform.eulerAngles.z);

thanks for the info, got it to work.