Sfrem/make rising from crouch slower (#9)
* change how transitions are handled * more work * add collision layers for player and environment * set 0.1 to be a constant
This commit is contained in:
@@ -7,6 +7,7 @@ const BASE_MOVE_SPEED: float = 5
|
||||
const SPRINT_MOVE_SPEED: float = 8
|
||||
const BASE_HEIGHT: float = 2.0
|
||||
const CROUCH_HEIGHT: float = 1.0
|
||||
const CROUCH_TRANSITION_SPEED: float = 0.1
|
||||
|
||||
enum MovementMode {
|
||||
Crouching,
|
||||
@@ -17,6 +18,7 @@ enum MovementMode {
|
||||
var current_movement := MovementMode.Walking
|
||||
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
var move_speed: float = BASE_MOVE_SPEED
|
||||
var target_height: float = BASE_HEIGHT
|
||||
|
||||
@onready var body_collision_shape: CollisionShape3D = $BodyCollisionShape
|
||||
@onready var fps_camera: Camera3D = $FPSCamera
|
||||
@@ -28,7 +30,7 @@ func _ready() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
head_collision_shape.set_deferred("disabled", true)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
func _process(_delta: float) -> void:
|
||||
if Input.is_action_just_pressed("pause"):
|
||||
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
@@ -36,33 +38,11 @@ func _process(delta: float) -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
velocity.y += -gravity * delta
|
||||
handle_state_transition()
|
||||
handle_movement(delta)
|
||||
|
||||
var input = Input.get_vector("strafe_left", "strafe_right",
|
||||
"move_forward", "move_backward")
|
||||
|
||||
if Input.is_action_pressed("sprint") \
|
||||
and current_movement != MovementMode.Crouching:
|
||||
move_speed = SPRINT_MOVE_SPEED
|
||||
current_movement = MovementMode.Sprinting
|
||||
elif Input.is_action_pressed("crouch") \
|
||||
and current_movement != MovementMode.Sprinting:
|
||||
move_speed = CROUCH_MOVE_SPEED
|
||||
current_movement = MovementMode.Crouching
|
||||
else:
|
||||
move_speed = BASE_MOVE_SPEED
|
||||
current_movement = MovementMode.Walking
|
||||
|
||||
if current_movement == MovementMode.Crouching:
|
||||
body_collision_shape.shape.height = CROUCH_HEIGHT
|
||||
head_collision_shape.set_deferred("disabled", false)
|
||||
elif not head_collider.has_overlapping_bodies():
|
||||
body_collision_shape.shape.height = BASE_HEIGHT
|
||||
head_collision_shape.set_deferred("disabled", true)
|
||||
|
||||
var movement_dir := transform.basis * Vector3(input.x, 0, input.y)
|
||||
velocity.x = movement_dir.x * move_speed
|
||||
velocity.z = movement_dir.z * move_speed
|
||||
body_collision_shape.shape.height = lerpf(body_collision_shape.shape.height,
|
||||
target_height, CROUCH_TRANSITION_SPEED)
|
||||
|
||||
move_and_slide()
|
||||
if is_on_floor() and Input.is_action_just_pressed("jump"):
|
||||
@@ -75,3 +55,55 @@ func _input(event: InputEvent) -> void:
|
||||
fps_camera.rotate_x(-event.relative.y * MOUSE_SENSITIVITY)
|
||||
fps_camera.rotation.x = clampf(fps_camera.rotation.x,
|
||||
-deg_to_rad(70), deg_to_rad(70))
|
||||
|
||||
func handle_movement(delta: float) -> void:
|
||||
velocity.y += -gravity * delta
|
||||
var input = Input.get_vector("strafe_left", "strafe_right",
|
||||
"move_forward", "move_backward")
|
||||
|
||||
var movement_dir := transform.basis * Vector3(input.x, 0, input.y)
|
||||
velocity.x = movement_dir.x * move_speed
|
||||
velocity.z = movement_dir.z * move_speed
|
||||
|
||||
func handle_state_transition() -> void:
|
||||
match current_movement:
|
||||
MovementMode.Crouching:
|
||||
if not Input.is_action_pressed("crouch") \
|
||||
and not head_collider.has_overlapping_bodies():
|
||||
change_state(MovementMode.Walking)
|
||||
MovementMode.Walking:
|
||||
if Input.is_action_pressed("crouch"):
|
||||
change_state(MovementMode.Crouching)
|
||||
elif Input.is_action_pressed("sprint"):
|
||||
change_state(MovementMode.Sprinting)
|
||||
MovementMode.Sprinting:
|
||||
if Input.is_action_pressed("crouch"):
|
||||
change_state(MovementMode.Crouching)
|
||||
elif not Input.is_action_pressed("sprint"):
|
||||
change_state(MovementMode.Walking)
|
||||
|
||||
func change_state(new_state: MovementMode) -> void:
|
||||
if new_state == current_movement:
|
||||
return
|
||||
|
||||
exit_state()
|
||||
current_movement = new_state
|
||||
enter_state()
|
||||
|
||||
func exit_state() -> void:
|
||||
match current_movement:
|
||||
MovementMode.Crouching:
|
||||
head_collision_shape.set_deferred("disabled", true)
|
||||
|
||||
func enter_state() -> void:
|
||||
match current_movement:
|
||||
MovementMode.Walking:
|
||||
move_speed = BASE_MOVE_SPEED
|
||||
target_height = BASE_HEIGHT
|
||||
MovementMode.Crouching:
|
||||
head_collision_shape.set_deferred("disabled", false)
|
||||
move_speed = CROUCH_MOVE_SPEED
|
||||
target_height = CROUCH_HEIGHT
|
||||
MovementMode.Sprinting:
|
||||
move_speed = SPRINT_MOVE_SPEED
|
||||
target_height = BASE_HEIGHT
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
[ext_resource type="Script" uid="uid://bhplq3tsshgsb" path="res://Player/player_controller.gd" id="1_biqn8"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_biqn8"]
|
||||
height = 1.0
|
||||
|
||||
[sub_resource type="BoxShape3D" id="BoxShape3D_biqn8"]
|
||||
|
||||
[node name="PlayerController" type="CharacterBody3D" unique_id=1462541278]
|
||||
collision_mask = 2
|
||||
script = ExtResource("1_biqn8")
|
||||
|
||||
[node name="BodyCollisionShape" type="CollisionShape3D" parent="." unique_id=1091416129]
|
||||
@@ -19,6 +19,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6, 0)
|
||||
|
||||
[node name="HeadCollider" type="Area3D" parent="." unique_id=2069486354]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.0033772, 0)
|
||||
collision_mask = 2
|
||||
|
||||
[node name="HeadCollisionShape" type="CollisionShape3D" parent="HeadCollider" unique_id=64452340]
|
||||
shape = SubResource("BoxShape3D_biqn8")
|
||||
|
||||
@@ -32,6 +32,8 @@ shadow_enabled = true
|
||||
transform = Transform3D(23.24995, 0, 0, 0, 0.12418556, 0, 0, 0, 12.13962, 0, 0, 2.206223)
|
||||
material_override = SubResource("StandardMaterial3D_10oj7")
|
||||
use_collision = true
|
||||
collision_layer = 2
|
||||
collision_mask = 3
|
||||
size = Vector3(1, 1, 2.2382813)
|
||||
|
||||
[node name="PlayerController" parent="." unique_id=1462541278 instance=ExtResource("1_2rmyx")]
|
||||
@@ -40,3 +42,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.06209278, 0)
|
||||
[node name="CSGBox3D2" type="CSGBox3D" parent="." unique_id=1350209690]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.7963257, -2.6601963)
|
||||
use_collision = true
|
||||
collision_layer = 2
|
||||
collision_mask = 3
|
||||
|
||||
@@ -26,6 +26,10 @@ config/icon="res://icon.svg"
|
||||
window/size/viewport_width=1920
|
||||
window/size/viewport_height=1080
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="The Tower"
|
||||
|
||||
[input]
|
||||
|
||||
move_forward={
|
||||
@@ -69,6 +73,11 @@ pause={
|
||||
]
|
||||
}
|
||||
|
||||
[layer_names]
|
||||
|
||||
3d_physics/layer_1="Player"
|
||||
3d_physics/layer_2="Environment"
|
||||
|
||||
[physics]
|
||||
|
||||
3d/physics_engine="Jolt Physics"
|
||||
|
||||
Reference in New Issue
Block a user