From a8f78bd4cde478030bd44076e9b429caa9a693d2 Mon Sep 17 00:00:00 2001 From: "s0wlz (Matthias Puchstein)" Date: Thu, 16 Apr 2026 00:25:08 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20add=20player=5Fcontroller=20=E2=80=94?= =?UTF-8?q?=20WASD=20movement,=20mouse=20look,=20interact=20raycast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruf-der-pilze/scripts/player_controller.gd | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ruf-der-pilze/scripts/player_controller.gd diff --git a/ruf-der-pilze/scripts/player_controller.gd b/ruf-der-pilze/scripts/player_controller.gd new file mode 100644 index 0000000..095fddd --- /dev/null +++ b/ruf-der-pilze/scripts/player_controller.gd @@ -0,0 +1,51 @@ +extends CharacterBody3D + +const SPEED := 4.0 +const GRAVITY := 9.8 +const MOUSE_SENSITIVITY := 0.002 +const INTERACT_DISTANCE := 2.5 + +var _camera: Camera3D +var _raycast: RayCast3D + + +func _ready() -> void: + _camera = get_node("Camera3D") as Camera3D + _raycast = _camera.get_node("InteractRay") as RayCast3D + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + + +func _physics_process(delta: float) -> void: + if not is_on_floor(): + velocity.y -= GRAVITY * delta + + var input_dir := Vector2( + Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), + Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward") + ) + var move_dir := (transform.basis.x * input_dir.x + -transform.basis.z * input_dir.y).normalized() + velocity.x = move_dir.x * SPEED + velocity.z = move_dir.z * SPEED + move_and_slide() + + +func _unhandled_input(event: InputEvent) -> void: + if event is InputEventMouseMotion: + rotate_y(-event.relative.x * MOUSE_SENSITIVITY) + _camera.rotate_x(-event.relative.y * MOUSE_SENSITIVITY) + _camera.rotation.x = clampf(_camera.rotation.x, -PI / 2.0, PI / 2.0) + elif event.is_action_pressed("interact"): + _try_interact() + elif event.is_action_pressed("ui_cancel"): + if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: + Input.mouse_mode = Input.MOUSE_MODE_VISIBLE + else: + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + + +func _try_interact() -> void: + if not _raycast.is_colliding(): + return + var target := _raycast.get_collider() + if target != null and target.has_method("interact"): + target.interact(NetworkManager.my_id)