diff --git a/scenes/player/player.gd b/scenes/player/player.gd index ce9b269..cbc7c9d 100644 --- a/scenes/player/player.gd +++ b/scenes/player/player.gd @@ -2,103 +2,116 @@ class_name Player extends CharacterBody3D -@export var camera_acceleration: float = 10.0 -@export var crouch_height: float = 0.95 ## Camera and RayCast height +@export var camera_acceleration: float = 10.0 ## The acceleration used when lerping the camera position when crouching and standing +@export var crouch_height: float = 0.8 ## This should be where the head/camera would be located +@export var jump_count: int = 1 ## Number of jumps, reset when on the floor +@export var gravity: float = 9.8 +@export var mouse_sensitivity: Vector2 = Vector2(0.1, 0.3) ## Horizontal and vertical mouse sensitivity +@export var standing_height: float = 1.8 ## This should be where the head/camera would be located + +@export_group("Movement") +@export var acceleration: float = 10.0 @export var crouch_speed: float = 3.0 @export var jump_velocity: float = 4.5 -@export var mouse_sensitivity_horizontal: float = 0.002 -@export var mouse_sensitivity_vertical: float = 0.002 -@export var run_speed: float = 8 -@export var standing_height: float = 1.8 ## Camera and RayCast height +@export var sprint_speed: float = 8.0 +@export var sprint_toggle: bool = false ## Enables toggle sprint instead of hold. When enabled, sprinting will also be disabled when velocity is zero @export var walk_speed: float = 5.0 -@onready var camera: Camera3D = $Camera -@onready var collision_shape_crouching: CollisionShape3D = $CollisionShapeCrouching +@onready var head: Node3D = $Head @onready var collision_shape_standing: CollisionShape3D = $CollisionShapeStanding -@onready var ray_cast_look: RayCast3D = $RayCastLook +@onready var collision_shape_crouching: CollisionShape3D = $CollisionShapeCrouching @onready var ray_cast_crouch: RayCast3D = $RayCastCrouch -var current_speed: float = 0 +var current_jump_count: int = 0 +var current_speed: float = walk_speed +var direction: Vector3 = Vector3.ZERO var is_crouching: bool = false -var is_running: bool = false - - -func _physics_process(delta: float) -> void: - is_crouching = Input.is_action_pressed("crouch") - - apply_gravity(delta) - handle_jumping() - set_is_running() - handle_movement() - handle_crouching(delta) +var is_sprinting: bool = false - move_and_slide() -func _unhandled_input(event: InputEvent) -> void: +func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: - handle_mouse_look(event) - + _handle_mouse_movement(event.relative) -func apply_gravity(delta: float) -> void: - if is_on_floor(): return - velocity += get_gravity() * delta +func _physics_process(delta: float) -> void: + is_crouching = Input.is_action_pressed("crouch") or ray_cast_crouch.is_colliding() + _set_is_sprinting() -## It's not generally recommended to modify the shape/size of a collision shape during runtime. -## For this reason, we have 2 collision shapes, with 1 disabled at all times -## along with the changing the height of the camera (using lerp for smoother movement) -func handle_crouching(delta: float) -> void: - if is_crouching: - collision_shape_crouching.disabled = false - collision_shape_standing.disabled = true - camera.transform.origin.y = lerp(camera.transform.origin.y, crouch_height, camera_acceleration * delta) - ray_cast_look.transform.origin.y = crouch_height - elif !ray_cast_crouch.is_colliding(): - collision_shape_crouching.disabled = true - collision_shape_standing.disabled = false - camera.transform.origin.y = lerp(camera.transform.origin.y, standing_height, camera_acceleration * delta) - ray_cast_look.transform.origin.y = standing_height - else: - pass # Continue crouching until nothing overhead + _apply_gravity(delta) + _handle_jumping() -func handle_jumping() -> void: - if not Input.is_action_just_pressed("jump"): return - if not is_on_floor(): return - velocity.y = jump_velocity + _set_current_speed() + _set_direction(delta) + _handle_movement(delta) + _handle_crouching(delta) -func handle_mouse_look(event: InputEvent) -> void: - rotation.y = rotation.y - event.relative.x * mouse_sensitivity_horizontal - camera.rotation.x = camera.rotation.x - event.relative.y * mouse_sensitivity_vertical - camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90)) - ray_cast_look.rotation.x = camera.rotation.x + move_and_slide() -func handle_movement() -> void: - var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward") - var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() - set_movement_speed() +func _apply_gravity(delta: float) -> void: + if is_on_floor(): return + velocity.y -= gravity * delta + +## Toggle the crouching and standing collision shapes[br] +## Set the head height to the crouching or standing height +func _handle_crouching(delta: float) -> void: + collision_shape_crouching.disabled = !is_crouching + collision_shape_standing.disabled = is_crouching + + var height: float = crouch_height if is_crouching else standing_height + head.position.y = lerp(head.position.y, height, camera_acceleration * delta) + +func _handle_jumping() -> void: + if is_on_floor(): + current_jump_count = 0 + + if not is_on_floor() and current_jump_count >= jump_count: + return + elif Input.is_action_just_pressed("jump"): + current_jump_count += 1 + velocity.y = jump_velocity + +## Rotate the player and head to follow mouse movement +func _handle_mouse_movement(mouse_position: Vector2) -> void: + rotate_y(deg_to_rad(-mouse_position.x * mouse_sensitivity.x)) # Rotate player horizontally + head.rotate_x(deg_to_rad(-mouse_position.y * mouse_sensitivity.y)) # Rotate head vertically + head.rotation.x = clamp(head.rotation.x, deg_to_rad(-90), deg_to_rad(90)) + +func _handle_movement(delta: float) -> void: if direction: velocity.x = direction.x * current_speed velocity.z = direction.z * current_speed else: - velocity.x = move_toward(velocity.x, 0, current_speed) - velocity.z = move_toward(velocity.z, 0, current_speed) - -## Determine if should be running. This needs to run prior to handle_movement(). -## Running is disabled when:[br] -## 1. The `run` input is pressed (ie toggled)[br] -## 2. Is no longer moving -func set_is_running() -> void: - if Input.is_action_just_pressed("run") and not is_running: - is_running = true - elif Input.is_action_just_pressed("run") and is_running: - is_running = false - if velocity == Vector3.ZERO: - is_running = false - -func set_movement_speed() -> void: - if is_crouching or ray_cast_crouch.is_colliding(): + velocity.x = move_toward(velocity.x, 0, current_speed * delta) + velocity.z = move_toward(velocity.z, 0, current_speed * delta) + +func _set_current_speed() -> void: + if !is_on_floor(): return # Keep speed in air + + if is_crouching: current_speed = crouch_speed - elif is_running: - current_speed = run_speed + elif is_sprinting: + current_speed = sprint_speed else: current_speed = walk_speed + +func _set_direction(delta: float) -> void: + var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward") + direction = lerp( + direction, + (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(), + acceleration * delta + ) + +func _set_is_sprinting() -> void: + if is_crouching: + is_sprinting = false + elif sprint_toggle: + if Input.is_action_just_pressed("run") and not is_sprinting: + is_sprinting = true # Toggled sprint on + elif Input.is_action_just_pressed("run") and is_sprinting: + is_sprinting = false # Toggled sprint off + elif velocity == Vector3.ZERO: + is_sprinting = false + else: + is_sprinting = Input.is_action_pressed("run") diff --git a/scenes/player/player.gd.uid b/scenes/player/player.gd.uid index 9a029d7..4fc5124 100644 --- a/scenes/player/player.gd.uid +++ b/scenes/player/player.gd.uid @@ -1 +1 @@ -uid://b6xiwiwwbdapd +uid://daly0bs0oat3j diff --git a/scenes/player/player.tscn b/scenes/player/player.tscn index fd3d139..e21297d 100644 --- a/scenes/player/player.tscn +++ b/scenes/player/player.tscn @@ -1,45 +1,41 @@ -[gd_scene load_steps=6 format=3 uid="uid://cggiju8k3bkoc"] +[gd_scene load_steps=5 format=3 uid="uid://beu1b4leqwgh3"] -[ext_resource type="Script" uid="uid://b6xiwiwwbdapd" path="res://scenes/player/player.gd" id="1_rp718"] -[ext_resource type="Script" uid="uid://bruuttf8j7wet" path="res://scenes/player/ray_cast_3d.gd" id="2_dovo2"] +[ext_resource type="Script" uid="uid://daly0bs0oat3j" path="res://scenes/player/player.gd" id="1_7sql3"] +[ext_resource type="Script" uid="uid://bruuttf8j7wet" path="res://scenes/player/ray_cast_look.gd" id="2_dovo2"] -[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_dovo2"] +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_tv3cd"] radius = 0.47 height = 1.95 -[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_gmlin"] +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_yk0hw"] radius = 0.2 height = 0.89 -[sub_resource type="CapsuleMesh" id="CapsuleMesh_gmlin"] - [node name="Player" type="CharacterBody3D"] collision_layer = 2 -script = ExtResource("1_rp718") +script = ExtResource("1_7sql3") +jump_count = 2 +sprint_toggle = true [node name="CollisionShapeStanding" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.953133, 0) -shape = SubResource("CapsuleShape3D_dovo2") +shape = SubResource("CapsuleShape3D_tv3cd") [node name="CollisionShapeCrouching" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.536112, 0) -shape = SubResource("CapsuleShape3D_gmlin") +shape = SubResource("CapsuleShape3D_yk0hw") disabled = true -[node name="MeshInstance" type="MeshInstance3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.953133, 0) -mesh = SubResource("CapsuleMesh_gmlin") - -[node name="Camera" type="Camera3D" parent="."] +[node name="Head" type="Node3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.8, 0) -[node name="RayCastLook" type="RayCast3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.8, 0) -target_position = Vector3(0, 0, -5) -debug_shape_custom_color = Color(1, 0, 0, 1) +[node name="Camera3D" type="Camera3D" parent="Head"] + +[node name="RayCastLook" type="RayCast3D" parent="Head"] +target_position = Vector3(0, 0, -10) +debug_shape_custom_color = Color(0.753415, 4.77457e-05, 0.578804, 1) script = ExtResource("2_dovo2") [node name="RayCastCrouch" type="RayCast3D" parent="."] editor_description = "Y should be just slightly higher than the height of the standing collision shape" target_position = Vector3(0, 1.96, 0) -debug_shape_custom_color = Color(1, 0, 0, 1) diff --git a/scenes/player/ray_cast_3d.gd b/scenes/player/ray_cast_look.gd similarity index 100% rename from scenes/player/ray_cast_3d.gd rename to scenes/player/ray_cast_look.gd diff --git a/scenes/player/ray_cast_3d.gd.uid b/scenes/player/ray_cast_look.gd.uid similarity index 100% rename from scenes/player/ray_cast_3d.gd.uid rename to scenes/player/ray_cast_look.gd.uid diff --git a/scenes/world/world.tscn b/scenes/world/world.tscn index fa84e3a..a1dcf5b 100644 --- a/scenes/world/world.tscn +++ b/scenes/world/world.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=6 format=3 uid="uid://mkfitwqnerku"] [ext_resource type="Script" uid="uid://c0jbvki2cylct" path="res://scenes/world/world.gd" id="1_6m72w"] -[ext_resource type="PackedScene" uid="uid://cggiju8k3bkoc" path="res://scenes/player/player.tscn" id="2_sl2e5"] +[ext_resource type="PackedScene" uid="uid://beu1b4leqwgh3" path="res://scenes/player/player.tscn" id="2_sl2e5"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_4rhad"] sky_horizon_color = Color(0.662243, 0.671743, 0.686743, 1)