- Edited
Hello, this is my first post on the Godot forums. I come with an issue regarding camera rotation for my first 3D project. I have
tried to create camera rotation, but now that I have clamped the rotation of the camera on the x axis, whenever the x axis rotation reaches either the max or the min of the clamp, it flips to the other end of the clamp (e.g. flipping from 85 to -85 and vice versa). I believe this may have something to do with the code still detecting input from the mouse after the value has reached one end of the clamp, but I am not sure how I would remedy this or if it is even the case. Please help! Code included below (If this helps, I am rotating the camera on the x axis, but the entire player on the y axis. I have tried printing values such as the rotation itself and mouse position to try to diagnose the issue but cannot figure it out):
extends CharacterBody3D
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var moveSpeed = 5;
var jumpForce = 5;
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(delta):
pass
func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta
if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = jumpForce
var inputDir = Input.get_vector("A", "D", "W", "S")
var direction = (transform.basis * Vector3(inputDir.x, 0, inputDir.y)).normalized()
if direction:
velocity.x = direction.x * moveSpeed
velocity.z = direction.z * moveSpeed
else:
velocity.x = move_toward(velocity.x, 0, moveSpeed)
velocity.z = move_toward(velocity.z, 0, moveSpeed)
move_and_slide()
var yaw = 0
var pitch = 0
var sensitivity = 0.0125
func _input(event):
if event is InputEventMouseMotion:
yaw += event.relative.x * -sensitivity
pitch += event.relative.y * -sensitivity
$Camera3D.transform.basis = Basis()
transform.basis = Basis()
rotate_object_local(Vector3(0, 1, 0), yaw)
$Camera3D.rotate_object_local(Vector3(1, 0, 0), pitch)
$Camera3D.rotation.x = clampf($Camera3D.rotation.x, deg_to_rad(-85), deg_to_rad(85))