Camera aggressive uncalled rotation

I have no clue if this is even the correct place to post, unity forums absolutely confuses the G out of me… Aaaaanyway!

I’m working on a game where the player will be mainly focusing on camera controlling, basically an upside down RPG of sorts if you will. It mostly works however, for one reason or another when I control my camera to be rotating and moving, it starts flicking aggressively down so it look directly at a 90 Degree angle on the ground, and I can’t for the life of me figure out what it may be. The related codes look like this.

void Update() {
  if (isRotating) {
    HandleMovement();
  }

  HandleRotation();
  HandleZoom();
  HandleMouseClick();
}

private void HandleMovement() {
    if (isRotating) {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 right = mainCam.transform.right;
        Vector3 forward = mainCam.transform.forward;
        Vector3 up = mainCam.transform.up;

        right.y = 0;
        forward.y = 0;

        right.Normalize();
        forward.Normalize();
        up.Normalize();
        float speed = moveSpeed;

        if (Input.GetKey(KeyCode.LeftShift)) {
            speed = moveSpeed * 4.5f;
        }

        Vector3 moveDirection = (forward * vertical + right * horizontal) * speed * Time.deltaTime;

        if (Input.GetKey(KeyCode.Space)) {
            moveDirection += up * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.LeftControl)) {
            moveDirection -= up * speed * Time.deltaTime;
        }

        transform.Translate(moveDirection, Space.World);
    }
}

private void HandleRotation() {
    if (Input.GetMouseButton(2)) {
        float horizontal = Input.GetAxis("Mouse X") * lookSpeed;
        float vertical = Input.GetAxis("Mouse Y") * lookSpeed;

        Vector3 eulerRotation = transform.eulerAngles;

        float newYaw = eulerRotation.y + horizontal;
        float newPitch = Mathf.Clamp(eulerRotation.x - vertical, -89f, 89f);

        transform.eulerAngles = new Vector3(newPitch, newYaw, 0f);
        Cursor.lockState = CursorLockMode.Locked;
        isRotating = true;
    } else {
        Cursor.lockState = CursorLockMode.None;
        isRotating = false;
    }
}

You can’t clamp a transform’s eulerAngles because they’re in the range of 0 to 360, not -180 to 180.

Fixed:

    float pitch;

    private void HandleRotation() 
    {
        if (Input.GetMouseButton(2))
        {
            float horizontal = Input.GetAxis("Mouse X") * lookSpeed;
            float vertical = Input.GetAxis("Mouse Y") * lookSpeed;

            pitch-=vertical;
            pitch=Mathf.Clamp(pitch,-89f,89f);

            float newYaw = transform.eulerAngles.y + horizontal;

            transform.eulerAngles = new Vector3(pitch, newYaw, 0f);
            Cursor.lockState = CursorLockMode.Locked;
            isRotating = true;
        } 
        else 
        {
            Cursor.lockState = CursorLockMode.None;
            isRotating = false;
        }
    }
1 Like

Hey! Moved this post to the Unity Engine section instead of Learn & Certification as it’s a bug in your game.