I can't figure out how to clamp it

I can’t figure out how to clamp my camera rotation.

    float CurY = Input.GetAxisRaw("Mouse X");
    Vector3 RotateY = new Vector3(0f, CurY, 0f) * TurnSpeed;

    float CurX = Input.GetAxisRaw("Mouse Y");

    Vector3 RotateX = new Vector3(CurX, 0f, 0f) * TurnSpeed;

    var cam = Cam.transform.rotation;


    cam.x = Mathf.Clamp(cam.x, -AngleLimit, AngleLimit);

You’re hitting 2 problems:

  • you are reading the camera rotation (line 8), but not writing it back to the camera (line 12 just modifies the value you read)
  • even if you were reading/writing it, the ‘rotation’ value is a quaternion. I think you’re probably thinking about the eulerAngles property as in the previous answer. However you’d still need to write it back.

I would try something like this:

     Vector3 RotateX = new Vector3(CurX, 0f, 0f) * TurnSpeed;

     Vector3 cam_rotation = Cam.transform.eulerAngles;
     cam_rotation.x = Mathf.Clamp(cam_rotation.x, -AngleLimit, AngleLimit);
     Cam.transform.eulerAngles = cam_rotation;

As an extra note, simply clamping angles doesn’t always yield the expected result, and I can’t tell what that is from your code. However what I’ve provided above should certainly limit the value that the x component of the camera’s eulerAngles property can take on.

cam is in fact Quaternion. Probably what you intended to do was cam.eulerAngles.x