How can I stop my FPS camera from rotating 360 degrees?

Hello everyone, I am pretty new to coding in general and even more so in Unity. I am making an fps game and I would to limit the rotation up and down. how can I accomplish this?

here is my FPSCamera Script:

private float xRotate, yRotate;
private Quaternion cameraQuaternion;

void FixedUpdate()
{
	cameraQuaternion = Quaternion.Euler (0, xRotate, 0);
	GameObject.Find ("Player").transform.rotation = cameraQuaternion;
	xRotate += Input.GetAxis ("Mouse X");
	yRotate += Input.GetAxis ("Mouse Y");
	transform.rotation = Quaternion.Euler (yRotate,xRotate,0);
}

You could simply use Min/Max:

private GameObject player;
private float xRotate, yRotate;

void Start() {
    player = GameObject.Find("Player");
}

void Update()
{
     xRotate = Mathf.Min(50, Mathf.Max(-50, xRotate + Input.GetAxis ("Mouse X")));
     yRotate += Input.GetAxis ("Mouse Y");
     player.transform.localRotation = Quaternion.Euler(0, xRotate, 0);
     transform.localRotation = Quaternion.Euler(yRotate, 0, 0);
}

Some notes:

  • Using localRotation is probably looking better, assuming your camera is a child of the player.
  • You should get the player once in the Start() method or assign it through the editor instead of calling Find() each frame.
  • Inputs should be processed in the Update(), not the FixedUpdate() as they are cleared per frame.