I have a problem with the sound

Godot Version

Godot 4


How can I add sound to walking, running, or jumping? I have watched many videos but it did not work for me. Please help

Easiest (but spaghetti code) way is to have walking, running, jumping booleans in your code that are changed accordingly when player state changes. Then just write a play_movement_sound() func that plays appropriate sound based on what state player is in.

A slightly more involved but far better way is to implement a State Machine design pattern. This page explains how and has an example with 3 states (idle, run, jump), so you can use that as basis and just add 4th state, walk. Each state would play it’s own sound after it was entered.

GDQuest also has an article on this, though more generic/high-level: How to implement a State Machine in Godot | Sandro Maglione

What exactly is the problem? You don’t know how to play sounds in Godot, or you aren’t certain how to select the right sound based on the player’s state?

It is still awkward, when adding sounds and projectiles, they can play an infinite loop for a short time, if youre not carefull… Using a state machine ( match state ) makes the code look really messy and confusing very fast, with too many states or booleans. I wish there was a better way to do it, like putting everything inside an array and play each animation frame individually… then play the sound / projectile at the end of the frame, and move to next frame… like a timeLine…