Rotation y camera goes beyond the set limit

Godot Version

4.2.2 mono

Question

Somebody know why happen this???


Try using a clamp function

camera_xrotation = Mathf.Clamp(camera_xrotation + deltaX,Mathf.DegToRad(-90f),Mathf.DegToRad(90f))

I have this third-person camera script that works very well, but I’ve only applied limitations to the X axis. I think I can help you with what you need:

using Godot;
using Godot.Collections;
using System;

public partial class CameraThirdPerson : Node3D
{

	[ExportGroup("Nodes target")]
	[Export] Node3D cameraTarget;
	[Export] Node3D playerPosition;

	[ExportGroup("Camera Settings")]
	[Export] int pitchMax = 50;
	[Export] int pitchMin = -50;
    [Export] float cameraSmoothSpeed;

	private Camera3D camera;


    float yaw;
	float pitch;

	float yawSensitivity = 0.002f;
	float pitchSensitivity = .002f;

	bool isHideMouse = true;

    public override void _Ready()
	{
		camera = GetNode<Camera3D>("/root/World/Camera/CameraTarget/Arm/Camera3D");
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
	{


		ApplayRotationCamera((float)delta);
        CameraSmoothFallow((float)delta);

		if(isHideMouse)
			Input.MouseMode = Input.MouseModeEnum.Captured;
		else
            Input.MouseMode = Input.MouseModeEnum.Visible;


        HideMouse();
    }


    private void CameraSmoothFallow(float delta) {
        Vector3 tempPosition = GlobalPosition;
		tempPosition = LerpVector3(tempPosition, /*new Vector3(playerPosition.GlobalPosition.X, Position.Y, playerPosition.GlobalPosition.Z)*/ playerPosition.GlobalPosition, cameraSmoothSpeed * delta);

		GlobalPosition = playerPosition.GlobalPosition;//new Vector3(playerPosition.GlobalPosition.X, Position.Y, playerPosition.GlobalPosition.Z);

    }

    public override void _Input(InputEvent @event) {

		if (@event is InputEventMouseMotion eventMouse && Input.MouseMode != 0) {

			yaw += -eventMouse.Relative.X * yawSensitivity;
			pitch += eventMouse.Relative.Y * pitchSensitivity;
		} 
	}

	void ApplayRotationCamera(float _delta) {
        float tempYaw = Mathf.Lerp(cameraTarget.Rotation.Y, yaw, _delta * 10);
        float tempPitch = Mathf.Lerp(cameraTarget.Rotation.X, pitch, _delta * 10);

        // 
        /* # Support For gamepad (Is necessary to create controllers with inputs) #
		float cameraInputX = Input.GetAxis("RationRight", "RotationLeft");
		float cameraInputY = Input.GetAxis("RationDown", "RotationUp");
		Vector2 cameraInputs = new Vector2(cameraInputX, cameraInputY);

        yaw += cameraInputs.X * yawSensitivity * 30;
        pitch += cameraInputs.Y * pitchSensitivity * 20;*/


        cameraTarget.Rotation = new Vector3(tempPitch, tempYaw, 0);
        pitch = Mathf.Clamp(pitch, Mathf.DegToRad(pitchMin), Mathf.DegToRad(pitchMax));

    }


	public Vector3 LerpVector3(Vector3 _from, Vector3 _to, float interpolation) {

		float x_Interpolation = Mathf.Lerp(_from.X, _to.X, interpolation);
		float y_Interpolation = Mathf.Lerp(_from.Y, _to.Y, interpolation);
		float z_Interpolation = Mathf.Lerp(_from.Z, _to.Z, interpolation);

		return new Vector3(x_Interpolation, y_Interpolation, z_Interpolation);
	}

	private void HideMouse() {
		if (Input.IsActionJustPressed("HideMouse")) {
			isHideMouse = !isHideMouse;
		}
	}

	public Camera3D Camera {get { return camera; } }
	public Node3D CameraTarget { get { return cameraTarget; } }

}

Captura de tela 2024-08-13 180951