I'm new to godot and I recently had a problem when adding footsteps in my game. The problem is that the footsteps play after the key is released and not when the key is pressed. To do that, I made an if sentence that says "If walking == true: $AudioStreamPlayer3d.play()". I already checked that the Walking variable is set to true when pressing W. I did a walking animation and it works perfectly, unlike the audio.

Here's a part of the code:

if Input.is_action_pressed("movement_forward"):
		input_movement_vector.y += 1
		Walking = true
		if $CameraPivot/Camera/scene/SpotLight.visible == true:
			$CameraPivot/Camera/scene/FlashLightAnimPlayer.play("FlashlightMove")
		elif  $CameraPivot/Camera/scene/SpotLight.visible == false and Input.is_action_just_pressed("Flashlight"):
			$CameraPivot/Camera/scene/FlashLightAnimPlayer.play_backwards("FlashLightHide")
			$CameraPivot/Camera/scene/FlashLightAnimPlayer.queue("FlashlightMove") 
if Input.is_action_just_released("movement_forward"):
		Walking = false
if Walking == true:
		$AudioStreamPlayer3D.play()

Thanks in advance < 3

  • My guess is that the audio does not have a chance to start playing its audio before $AudioStreamPlayer3D.play() is called again, resetting the audio to the start. An easy fix should be just checking to see if the audio is playing already:

    # other code
    if Walking == true:
    	if $AudioStreamPlayer3D.is_playing() == false:
    		$AudioStreamPlayer3D.play()

My guess is that the audio does not have a chance to start playing its audio before $AudioStreamPlayer3D.play() is called again, resetting the audio to the start. An easy fix should be just checking to see if the audio is playing already:

# other code
if Walking == true:
	if $AudioStreamPlayer3D.is_playing() == false:
		$AudioStreamPlayer3D.play()

Thanks a lot, it works perfectly!

2 years later