28 lines
648 B
GDScript
28 lines
648 B
GDScript
class_name AudioComponent
|
|
extends Node
|
|
|
|
@export var movement: MovementComponent
|
|
@export var footsteps: Array[AudioStream]
|
|
@export var interval: float = 0.3
|
|
|
|
var audio_player: AudioStreamPlayer3D
|
|
var last_footstep: float
|
|
|
|
func _ready():
|
|
audio_player = $Footsteps
|
|
|
|
func _process(_delta):
|
|
if Time.get_unix_time_from_system() - last_footstep < interval:
|
|
return
|
|
|
|
if movement.direction == Vector3.ZERO:
|
|
return
|
|
|
|
if !movement.body.is_on_floor():
|
|
return
|
|
|
|
audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)]
|
|
audio_player.pitch_scale = randf_range(0.9, 1.1)
|
|
audio_player.play()
|
|
last_footstep = Time.get_unix_time_from_system()
|