Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Clamping Y rotation?

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

  1. fikonunity

    fikonunity

    Joined:
    Jan 7, 2022
    Posts:
    1
    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.

    Script:

    // 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);
    }