Godot Version
4.3
Question
Hello again i was experimenting with my code and wanted to make footstep timers and call them in code and when they end it plays a footstep sound
func _on_walk_timer_timeout() -> void:
$steps.play()
func _on_run_timer_timeout() -> void:
$steps.play()
i got the walk timer to work but ive been struggling with the run timer and have not found a good place to call the timer
sprinting = Input.is_action_pressed("pm_sprint")
current_speed = SPRINT_SPEED if sprinting else SPEED
if direction:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
$Walk_Timer.start()
velocity.x = move_toward(velocity.x, 0, current_speed)
velocity.z = move_toward(velocity.z, 0, current_speed)
Perhaps something like:
sprinting = Input.is_action_pressed("pm_sprint")
if sprinting:
if current_speed != SPRINT_SPEED:
current_speed = SPRINT_SPEED
$Walk_Timer.stop()
$Sprint_Timer.start()
else:
if current_speed != SPEED
current_speed = SPEED
$Sprint_Timer.stop()
$Walk_Timer.start()
if direction:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
velocity.x = move_toward(velocity.x, 0, current_speed)
velocity.z = move_toward(velocity.z, 0, current_speed)
Personally, I’d probably track the intended speed with an enum; it would make it easier to do things like add sneaking or jogging later. Something like:
enum MoveType { standing, sneaking, walking, jogging, sprinting }
var MoveAction = MoveType.standing
[...]
var new_move_action = _check_player_input() # Sprint or sneak pressed?
if new_move_action != MoveAction:
match(new_move_action):
MoveType.standing: # Transition to standing...
[...]
MoveType.sneaking: # Transition to sneaking...
[...]
[..]
I believe this is one of those cases where you are better off tying your footstep with the animation. You could call the function to play footstep from an animation player every time your characters foot moves.
Footsteps can actually be a bit of a problem if you have multiple characters. I worked on a brawler once that had up to 16 characters on-screen at any given time, all making anim-triggered footstep sounds when they moved. The rain of feet drowned out everything else and we had to dial it back severely. IIRC in the end we clamped it at a maximum of 3 people making footstep sounds at any given time.