From a79b24d625c5b9f0df9861b6d20a32093a755f49 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 27 Jan 2024 13:36:33 +0100 Subject: [PATCH] Change player from RigidBody3D to CharacterBody3D --- scenes/Player.tscn | 8 +++----- scenes/World.tscn | 8 ++++---- scripts/Player.gd | 24 ++++++++++++++---------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/scenes/Player.tscn b/scenes/Player.tscn index d8eaadc..e807bb6 100644 --- a/scenes/Player.tscn +++ b/scenes/Player.tscn @@ -2,15 +2,13 @@ [ext_resource type="Script" path="res://scripts/Player.gd" id="1_ady3x"] -[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_568l5"] +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_kcbxg"] -[node name="Player" type="RigidBody3D"] -lock_rotation = true -linear_damp = 3.0 +[node name="Player" type="CharacterBody3D"] script = ExtResource("1_ady3x") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] -shape = SubResource("CapsuleShape3D_568l5") +shape = SubResource("CapsuleShape3D_kcbxg") [node name="Head" type="Node3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0) diff --git a/scenes/World.tscn b/scenes/World.tscn index ad685b2..9c4a3ec 100644 --- a/scenes/World.tscn +++ b/scenes/World.tscn @@ -43,13 +43,13 @@ mesh = SubResource("PlaneMesh_vfc5y") shape = SubResource("ConcavePolygonShape3D_ibbj3") [node name="Player" parent="." instance=ExtResource("1_r5ocp")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) -[node name="Bureau3" type="MeshInstance3D" parent="."] +[node name="Bureau" type="MeshInstance3D" parent="."] transform = Transform3D(0.015, 0, 0, 0, 0.015, 0, 0, 0, 0.015, 0.145715, 0.531814, -3.61084) mesh = ExtResource("2_b86p5") -[node name="StaticBody3D" type="StaticBody3D" parent="Bureau3"] +[node name="StaticBody3D" type="StaticBody3D" parent="Bureau"] -[node name="CollisionShape3D" type="CollisionShape3D" parent="Bureau3/StaticBody3D"] +[node name="CollisionShape3D" type="CollisionShape3D" parent="Bureau/StaticBody3D"] shape = SubResource("ConcavePolygonShape3D_m6gwj") diff --git a/scripts/Player.gd b/scripts/Player.gd index 63610de..a61a19f 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -1,13 +1,13 @@ -extends RigidBody3D +extends CharacterBody3D @onready var head := $Head -const SPEED := 1200.0 +const SPEED := 5.0 const MOUSE_SENSITIVITY := 0.15 # Called when the node enters the scene tree for the first time. func _ready(): - # Change input mode + # Trap the cursor on start Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Called at each input @@ -21,13 +21,17 @@ func _input(event): # Trap the cursor on click Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - # Movement - var input := Vector3.ZERO - input.x = Input.get_axis("move_left", "move_right") - input.z = Input.get_axis("move_forward", "move_backward") - apply_central_force(basis * input.normalized() * SPEED * delta) +func _physics_process(delta): + # Get the input direction and handle the movement/deceleration. + var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward") + var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + velocity.x = direction.x * SPEED + velocity.z = direction.z * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + velocity.z = move_toward(velocity.z, 0, SPEED) + move_and_slide() # Free cursor if Input.is_action_just_pressed("ui_cancel"):