mtd/PCs/view_movement.gd

69 lines
2.1 KiB
GDScript3
Raw Normal View History

extends Node3D
class_name ViewMovement
@export var player : Hero
@export_category("Bobbing")
@export var head_bob_camera : Camera3D
@export var head_bob_focus_raycast : RayCast3D
@export var enable_head_bob := true
@export var head_bob_amplitude := 0.040
@export var head_bob_frequency := 0.015
@export var target_stabilisation := false
@export_category("Tilting")
@export var enable_tilt := true
@export var tilt_amount := 0.02
func _process(delta: float) -> void:
if enable_head_bob:
check_motion(delta)
if target_stabilisation:
head_bob_camera.look_at(focus_target())
if enable_tilt:
tilt_cam(delta)
func check_motion(delta) -> void:
var speed = player.velocity.length_squared()
if speed < 2.0:
reset_position(delta)
return
if !player.is_on_floor():
reset_position(delta)
return
play_motion(sample_lemniscate(Time.get_ticks_msec() * head_bob_frequency), delta)
func reset_position(delta) -> void:
if head_bob_camera.position != Vector3.ZERO:
head_bob_camera.position = lerp(head_bob_camera.position, Vector3.ZERO, 7.0 * delta)
func sample_lemniscate(t: float) -> Vector2:
var pos := Vector2.ZERO
pos.y += sin(t) * head_bob_amplitude
pos.x += cos(t / 2.0) * head_bob_amplitude * 2.0
return pos
func play_motion(motion: Vector2, delta) -> void:
var motion3d = Vector3(motion.x, motion.y, 0.0)
head_bob_camera.position = lerp(head_bob_camera.position, motion3d, 7.0 * delta)
func focus_target() -> Vector3:
if head_bob_focus_raycast.is_colliding():
return head_bob_focus_raycast.get_collision_point()
else:
return head_bob_focus_raycast.global_position + -head_bob_focus_raycast.global_transform.basis.z * 15.0
func tilt_cam(delta):
if player.velocity.length() < 1.0:
return
var dot = player.velocity.normalized().dot(-global_transform.basis.z)
dot = -abs(dot) + 1
var input_vector = Input.get_vector("Move Left", "Move Right", "Move Forward", "Move Backward")
head_bob_camera.rotation.z = lerp(head_bob_camera.rotation.z, tilt_amount * dot * -input_vector.x, 8.0 * delta)
#head_bob_camera.rotation.x = lerp(head_bob_camera.rotation.x, tilt_amount * dot * input_vector.y, 8.0 * delta)