Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Clamping Y rotation?

Discussion in 'Scripting' started by fikonunity, Nov 27, 2022.

  1. fikonunity


    Jan 7, 2022
    I am trying to make a camera look around similar to the original FNAF game, but I can't figure out how to clamp the y rotation. I tried doing it the same way as the x-clamping is working but it gave me a static jittery camera that was just shaking when I tried to look around. Help? I used the brackeys "First person movement" tutorial as a base.


    // Sensitivity
    public float sensitivity = 100f;

    // The Camera Transform
    public Transform playerCam;

    float xRotation = 0f;

    void Update()
    float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -5f, 5f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerCam.Rotate(Vector3.up * mouseX);