diff --git a/src/player/player.gd b/src/player/player.gd index 0bbb9b7..9c619a0 100644 --- a/src/player/player.gd +++ b/src/player/player.gd @@ -1,31 +1,48 @@ extends CharacterBody3D - -const SPEED = 5.0 -const JUMP_VELOCITY = 4.5 +const TARGET_FPS: float = 60.0 +const BASE_SPEED: float = 5.0 +const JUMP_FORCE: float = 4.5 +const FRICTION: float = 0.3 +const AIR_DRAG: float = 0.03 +const INPUT_SENSITIVITY: float = 0.7 +const TURN_SENSITIVITY: float = 0.08 # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") +@onready var camera_root: Node3D = $CameraRoot +@onready var mesh: Node3D = $Mesh + func _physics_process(delta: float) -> void: + var delta_factor: float = delta * TARGET_FPS + # Add the gravity. if not is_on_floor(): velocity.y -= gravity * delta # Handle jump. if Input.is_action_just_pressed("jump") and is_on_floor(): - velocity.y = JUMP_VELOCITY + velocity.y = JUMP_FORCE # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir := Input.get_vector("left", "right", "forward", "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) + var input_dir: Vector2 = Input.get_vector("left", "right", "forward", "backward") + if input_dir: + var movement: Vector3 = ( + camera_root.global_transform.basis * Vector3(input_dir.x, 0, input_dir.y) * -1 + ).normalized() + velocity.x = lerpf(velocity.x, movement.x * BASE_SPEED, delta_factor * INPUT_SENSITIVITY) + velocity.z = lerpf(velocity.z, movement.z * BASE_SPEED, delta_factor * INPUT_SENSITIVITY) + + mesh.rotation.y = lerp_angle( + mesh.rotation.y, + atan2(movement.x, movement.z), + delta_factor * TURN_SENSITIVITY + ) + + var drag: float = FRICTION if is_on_floor() else AIR_DRAG + velocity.x = lerpf(velocity.x, 0.0, delta_factor * drag) + velocity.z = lerpf(velocity.z, 0.0, delta_factor * drag) move_and_slide() diff --git a/src/player/player.tscn b/src/player/player.tscn index e078da5..b8c10a2 100644 --- a/src/player/player.tscn +++ b/src/player/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=3 uid="uid://bqp5mwyat3asv"] +[gd_scene load_steps=7 format=3 uid="uid://bqp5mwyat3asv"] [ext_resource type="Script" path="res://src/player/player.gd" id="1_h16ke"] [ext_resource type="Texture2D" uid="uid://b0yefajw1cqo" path="res://assets/textures/character_placeholder.png" id="1_iys0p"] @@ -11,8 +11,6 @@ albedo_texture = ExtResource("1_iys0p") [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_otw5j"] -[sub_resource type="SphereShape3D" id="SphereShape3D_jv2hv"] - [node name="Player" type="CharacterBody3D"] script = ExtResource("1_h16ke") @@ -27,13 +25,12 @@ surface_material_override/0 = SubResource("StandardMaterial3D_dd4i1") shape = SubResource("CapsuleShape3D_otw5j") [node name="CameraRoot" type="Node3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 0.5, 0) +transform = Transform3D(1, 0, 0, 0, 0.965926, -0.258819, 0, 0.258819, 0.965926, 0, 1, 0) script = ExtResource("3_gichr") [node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"] -transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0, 0, 0) -shape = SubResource("SphereShape3D_jv2hv") -spring_length = 3.0 +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1, 0, 0) +spring_length = -1.5 [node name="Camera3D" type="Camera3D" parent="CameraRoot/SpringArm3D"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3) +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -1.5) diff --git a/src/player/third_person_camera.gd b/src/player/third_person_camera.gd index 7885b23..2b0c904 100644 --- a/src/player/third_person_camera.gd +++ b/src/player/third_person_camera.gd @@ -10,7 +10,7 @@ extends Node3D @export_range(-90.0, 90.0) var pitch_max: float = 75.0 @export var invert_pitch: bool = false -@onready var _target: Vector2 = Vector2(rotation_degrees.x, rotation_degrees.y) +@onready var _target: Vector2 = Vector2(global_rotation_degrees.x, rotation_degrees.y) func _ready() -> void: Input.mouse_mode = Input.MOUSE_MODE_CAPTURED