Improved footstep audio

This commit is contained in:
Eduard Urbach 2024-02-14 20:29:52 +01:00
parent 37e8852c51
commit 07f356a8cc
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 43 additions and 13 deletions

View File

@ -96,7 +96,8 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
strength = 15.0 strength = 15.0
radius = 2.0 radius = 2.0
[node name="Audio" parent="." node_paths=PackedStringArray("movement") instance=ExtResource("17_ovrf7")] [node name="Audio" parent="." node_paths=PackedStringArray("skeleton", "movement") instance=ExtResource("17_ovrf7")]
skeleton = NodePath("../Model/Female/Armature/GeneralSkeleton")
movement = NodePath("../Movement") movement = NodePath("../Movement")
footsteps = Array[AudioStream]([ExtResource("12_h5nrh"), ExtResource("21_a8ikg"), ExtResource("14_6dv65"), ExtResource("15_1rjwu"), ExtResource("16_d8yn0"), ExtResource("17_74v1j"), ExtResource("18_qkget"), ExtResource("19_vvmo0"), ExtResource("20_srjtb"), ExtResource("21_a8ikg")]) footsteps = Array[AudioStream]([ExtResource("12_h5nrh"), ExtResource("21_a8ikg"), ExtResource("14_6dv65"), ExtResource("15_1rjwu"), ExtResource("16_d8yn0"), ExtResource("17_74v1j"), ExtResource("18_qkget"), ExtResource("19_vvmo0"), ExtResource("20_srjtb"), ExtResource("21_a8ikg")])

View File

@ -1,27 +1,52 @@
class_name AudioComponent class_name AudioComponent
extends Node extends Node
@export var skeleton: Skeleton3D
@export var movement: MovementComponent @export var movement: MovementComponent
@export var footsteps: Array[AudioStream] @export var footsteps: Array[AudioStream]
@export var interval: float = 0.3 @export var raise_threshold := 0.25
@export var step_threshold := 0.15
var audio_player: AudioStreamPlayer3D var feet_names := ["LeftFoot", "RightFoot"]
var last_footstep: float var feet := []
var feet_audio := []
var feet_raised := []
var feet_position := []
func _ready(): func _ready():
audio_player = $Footsteps for foot_name in feet_names:
var bone_id := skeleton.find_bone(foot_name)
var audio := get_node(foot_name)
feet.append(bone_id)
feet_audio.append(audio)
feet_raised.append(false)
feet_position.append(Vector3.ZERO)
var attachment := BoneAttachment3D.new()
attachment.bone_name = foot_name
attachment.bone_idx = bone_id
audio.reparent(attachment)
skeleton.add_child(attachment)
func _process(_delta): func _process(_delta):
if Time.get_unix_time_from_system() - last_footstep < interval: print()
return for i in range(feet.size()):
feet_position[i] = skeleton.get_bone_global_pose(feet[i]).origin
if movement.direction == Vector3.ZERO: print(feet_position[i].y)
return
if feet_position[i].y > raise_threshold:
feet_raised[i] = true
if !movement.body.is_on_floor(): if !movement.body.is_on_floor():
return return
for i in range(feet.size()):
if feet_raised[i] && feet_position[i].y < step_threshold:
play(feet_audio[i])
feet_raised[i] = false
func play(audio_player: AudioStreamPlayer3D):
audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)] audio_player.stream = footsteps[randi_range(0, footsteps.size()-1)]
audio_player.pitch_scale = randf_range(0.9, 1.1) audio_player.pitch_scale = randf_range(0.9, 1.1)
audio_player.play() audio_player.play()
last_footstep = Time.get_unix_time_from_system()

View File

@ -5,6 +5,10 @@
[node name="Audio" type="Node3D"] [node name="Audio" type="Node3D"]
script = ExtResource("1_0j3jl") script = ExtResource("1_0j3jl")
[node name="Footsteps" type="AudioStreamPlayer3D" parent="."] [node name="LeftFoot" type="AudioStreamPlayer3D" parent="."]
max_distance = 20.0
bus = &"Footsteps"
[node name="RightFoot" type="AudioStreamPlayer3D" parent="."]
max_distance = 20.0 max_distance = 20.0
bus = &"Footsteps" bus = &"Footsteps"