Mathf Clamp seems to have no effect

Hi,

This is for a first person player controller script.

I have horizontal rotation setup like so on the player object transform:

    private void rotateCameraHorizontal(float mouseInputX)
    {
        Quaternion rotation = transform.rotation; //get current rotation

        rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y + mouseInputX, rotation.eulerAngles.z);
        transform.rotation = rotation; //set new rotation
    }

I have vertical rotation setup on the player camera:

private void rotateCameraVertical(float mouseInputY)
    {
    float clampedYInput = Mathf.Clamp(mouseInputY, -90f, 90f); //this doesnt seem to affect anything.

    ObjectToApplyCameraTransform.rotation =     Quaternion.Euler(ObjectToApplyCameraTransform.rotation.eulerAngles + new Vector3(clampedYInput, 0f, 0f));
    }

The clamping does not seem to make a difference? Any advice?

If mouseInputY is simply the input from the mouse axis, it is not going to get anywhere near 90 + or -. How is that value being calculated before being sent to the method?

Edit: Yes, mouse input is just the input from the mouse axis. How should I be going about this? At the moment the mouse is limited to 90 degrees up and down but if you repeatedly force the mouse against either “north/south pole” the screen will go upside down.

    private Vector2 getRawMouseInput()
    {
        Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;

        mouseInput.y = invertY ? mouseInput.y : -mouseInput.y; //invert Y check

        return mouseInput;
    }

    private void moveCamera()
    {
        Vector2 mouseInput = getRawMouseInput();
        rotateCameraHorizontal(mouseInput.x);
        rotateCameraVertical(mouseInput.y);
    }

put a Debug.Log(mouseInput.y) in your moveCamera method and see if it ever gets anywhere near 90

1 Like

Ah right, its simply input being added to the rotation. Hmmm. Can you advise on how I should go about this? I edited my previous comment with the upside down issue btw.

Am I doing XY rotation properly then? Should I be rotating the camera and transform separately like this, it’s just something I gathered from watching a bunch of FPS scripts and found Mathf.Clamp used by some scripts but I must have misunderstood them.