Files
godot-demo-projects/3d/waypoints/camera.gd
Hugo Locurcio 5f4a9e409f Use InputEventMouseMotion.screen_relative where relevant in all demos (#1232)
This provides mouse sensitivity that is independent of the viewport
size, without needing to query for project settings.

This also inverts the mouse motion direction in the 3D navigation demo
to better match expectations, and increases mouse sensitivity in the
Window Management demo to be closer to other demos.
2025-10-01 18:54:19 -07:00

43 lines
1.2 KiB
GDScript

extends Camera3D
const MOUSE_SENSITIVITY = 0.002
const MOVE_SPEED = 0.65
var rot := Vector3()
var velocity := Vector3()
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event: InputEvent) -> void:
# Mouse look (only if the mouse is captured).
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
# Horizontal mouse look.
rot.y -= event.screen_relative.x * MOUSE_SENSITIVITY
# Vertical mouse look.
rot.x = clamp(rot.x - event.screen_relative.y * MOUSE_SENSITIVITY, -1.57, 1.57)
transform.basis = Basis.from_euler(rot)
if event.is_action_pressed(&"toggle_mouse_capture"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(delta: float) -> void:
var motion := Vector3(
Input.get_axis(&"move_left", &"move_right"),
0,
Input.get_axis(&"move_forward", &"move_back")
)
# Normalize motion to prevent diagonal movement from being
# `sqrt(2)` times faster than straight movement.
motion = motion.normalized()
velocity += MOVE_SPEED * delta * (transform.basis * motion)
velocity *= 0.85
position += velocity