add camera effects for running and crouching

This commit is contained in:
Teatov 2025-02-13 19:43:21 +10:00
parent 64ba702ac3
commit 2867918c92
2 changed files with 26 additions and 0 deletions

View File

@ -4,6 +4,10 @@ const SETTING_USAGE_FLAG := PROPERTY_USAGE_SCRIPT_VARIABLE | PROPERTY_USAGE_EDIT
const CONFIG_PATH := "user://settings.cfg"
@export_group("Gameplay")
@export_subgroup("Camera")
@export var camera_fov: float = 75:
set(value):
camera_fov = value
@export_group("Video")
@export_subgroup("Display")

View File

@ -11,10 +11,16 @@ const JUMP_FORCE: float = 10
const FALL_SPEED: float = 20
const FALL_ACCELERATION: float = 25
const CAMERA_CROUCH_HEIGHT: float = 1
const CAMERA_HEIGHT_SPEED: float = 5
const CAMERA_RUN_EXTRA_FOV: float = 5
const CAMERA_FOV_SPEED: float = 50
@export var _respawn_height: float = -100
var _peer_id: int = 1
var _respawn_point: Vector3
var _default_camera_height: float
@onready var _camera: Camera3D = $Camera3D
@onready var _name_label: Label3D = $NameLabel
@ -31,6 +37,7 @@ func _ready() -> void:
if not is_multiplayer_authority():
return
_default_camera_height = _camera.position.y
_respawn_point = global_position
_camera.make_current()
Referencer.player = self
@ -59,6 +66,7 @@ func _process(delta: float) -> void:
_process_respawning()
_process_controller_rotating(delta)
_process_camera(delta)
func _physics_process(delta: float) -> void:
@ -122,6 +130,20 @@ func _process_controller_rotating(delta: float) -> void:
_camera.rotation.x = clampf(_camera.rotation.x, PI / -2, PI / 2)
func _process_camera(delta: float) -> void:
var camera_height := _default_camera_height
if Input.is_action_pressed("crouch"):
camera_height = CAMERA_CROUCH_HEIGHT
_camera.position.y = move_toward(
_camera.position.y, camera_height, CAMERA_HEIGHT_SPEED * delta
)
var camera_fov := Settings.camera_fov
if Input.is_action_pressed("run"):
camera_fov += CAMERA_RUN_EXTRA_FOV
_camera.fov = move_toward(_camera.fov, camera_fov, CAMERA_FOV_SPEED * delta)
func _lateral_movement(delta: float) -> void:
var input_dir := Input.get_vector(
"move_left", "move_right", "move_forward", "move_back"