mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-31 09:49:06 +03:00
* General proofreading for grammar and spelling * General formatting * Addition of appropriate literals where appropriate, i.e. `&"foo"` for `StringName` cases and `^"foo/bar"` for `NodePath` cases
44 lines
1.3 KiB
GDScript
44 lines
1.3 KiB
GDScript
extends Camera3D
|
|
|
|
const MOUSE_SENSITIVITY = 0.002
|
|
const MOVE_SPEED = 10.0
|
|
|
|
var rot := Vector3()
|
|
var velocity := Vector3()
|
|
|
|
|
|
func _ready() -> void:
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
|
func _input(input_event: InputEvent) -> void:
|
|
# Mouse look (only if the mouse is captured, and only after the loading screen has ended).
|
|
if input_event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and Engine.get_process_frames() > 2:
|
|
# Horizontal mouse look.
|
|
rot.y -= input_event.relative.x * MOUSE_SENSITIVITY
|
|
# Vertical mouse look.
|
|
rot.x = clampf(rot.x - input_event.relative.y * MOUSE_SENSITIVITY, -1.57, 1.57)
|
|
transform.basis = Basis.from_euler(rot)
|
|
|
|
if input_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
|