Make Games with Godot 4 and AI · Lesson 8 of 11

Godot 4 Sound, Music & Game Feel with Tweens (Lesson 8)

A "boing" when you jump, a "ching" and sparkles when you grab a coin, a screen shake when an enemy hits you. You'll add sound effects, BGM, Tweens, and particles to give the game a satisfying feel. The sound files are available to download.

In Lesson 7, we completed one full loop of the game, from the title screen through clearing the stage or getting a game over. The rules are all in place, but there's no sound yet, and coins just blink out of existence. In this lesson, we'll use sound effects, BGM, Tweens, and particles to make the game feel good to play.

Finished project for Lesson 8: project-lesson08.zip (380 KB)

The game screen at the moment the player grabs a coin on the ground. The coin grows as it fades, yellow square specks scatter around it, and the "Coins: 1 / 6" text in the top left glows yellow
The moment you grab a coin. It swells as it disappears, sparkles fly, and the score text lights up

What is "game feel"?

Two games with the exact same rules can feel completely different to play, depending on whether they have small touches of sound and motion. In game development, this sensation is called game feel (adding those little touches is also called "adding juice").

The trick is to respond to the player's actions immediately and clearly. Here's a table of the touches we'll add in this lesson. Each one takes just a few lines of code.

EventTouch we addWhat we use
JumpedA springy "boing" soundAudioStreamPlayer
LandedThe character squashes for an instantTween
Grabbed a coinA sound, the coin swells and vanishes, sparkles, the score lights upAudioStreamPlayer, Tween, CPUParticles2D
Touched an enemyA sound, the screen shakes, the lives display lights upAudioStreamPlayer, Tween
The game endedThe BGM fades down, and the screen switches after 1 secondTween, a timer

That said, more effects aren't always better. Too much shaking or flashing actually makes a game harder to play. The numbers in this lesson are kept modest, so adjust them to your taste later.

Get the sound files

We'll use sound effects and BGM made for this series. They're all original sounds generated from waveforms by a program, so you're free to use them. Download all four and put them in your project folder (the same place as project.godot).

Sound effects: jump.wav (jump, 7 KB) · coin.wav (coin, 15 KB) · hit.wav (getting hit, 14 KB)
BGM: bgm.wav (a loop that cycles every 8 seconds, 345 KB)

Back in Godot, the four .wav files show up in the FileSystem dock. Besides WAV, Godot can also load Ogg Vorbis (.ogg) and MP3. The usual approach is WAV for short sound effects and Ogg Vorbis for long BGM, since it makes smaller files (this time we've used WAV for the BGM too, to keep things simple).

Make the BGM loop

We want the BGM to go back to the beginning when it reaches the end, so we'll change its import settings.

  1. Select bgm.wav in the FileSystem dock and open the Import tab at the top left.
  2. Under Edit, set Loop Mode to Forward (start over from the beginning when it reaches the end).
  3. Click Reimport at the bottom.

This setting is saved as edit/loop_mode=2 in a file called bgm.wav.import. The track is made so its end flows cleanly into its beginning, so you shouldn't hear the seam.

Set up audio buses

An audio bus is a "channel" that groups sounds together. If you send sound effects through a bus called SFX and BGM through one called BGM, then "turn down just the BGM" or "mute just the sound effects" is as simple as changing a bus's volume. You can also reuse this as is when you build volume settings into an options screen later.

  1. Open the Audio tab at the bottom of the editor. There's just one bus, called Master. Every sound passes through it last on its way to the speakers.
  2. Click Add Bus twice.
  3. Double-click the name of each new bus and rename the first one SFX and the second one BGM.
  4. Lower the volume slider on the BGM bus to -8 dB. Keeping the BGM a little quieter stops it from drowning out the sound effects.

These settings are saved automatically to the project's default_bus_layout.tres.

Getting a feel for dB (decibels)

Volume is set in dB. 0 is the original loudness, -6 sounds roughly half as loud, and at -40 you can barely hear it. Positive values make it louder than the original, but the sound distorts easily, so as a rule, adjust at 0 or below.

Sound effects for jumping and getting hit

The node that plays sounds is AudioStreamPlayer. Set the sound file you want to play as its Stream, then call play() from code.

  1. Open player.tscn, add an AudioStreamPlayer as a child of Player, and name it JumpSound. Drag jump.wav onto Stream in the Inspector, and set Bus to SFX.
  2. In the same way, add another AudioStreamPlayer named HitSound, with Stream set to hit.wav and Bus set to SFX.
player.tscn open with JumpSound (AudioStreamPlayer) selected. In the Inspector, Stream is set to jump.wav and Bus to SFX
JumpSound in the Inspector. Set the sound file in Stream and the bus it plays through in Bus

An AudioStreamPlayer plays at the same volume no matter where things are on screen. When you want a sound's volume to depend on where it comes from, like a sound coming from an enemy's position, you use AudioStreamPlayer2D, but on a small stage like this one you don't need to bother.

In code, all you add is $JumpSound.play() at the moment of the jump and $HitSound.play() when you touch an enemy. We'll show the whole player.gd later, once we've added landing and screen shake.

Make grabbing a coin feel great

Right now, a coin vanishes via queue_free() the instant you touch it. We want to add effects here, but deleting a node also deletes its child sounds and particles. So we'll change the order to "when collected, play the effects first, then disappear once they're done."

Add a sound and sparkles to the coin

  1. Open coin.tscn, add an AudioStreamPlayer as a child of Coin, name it CoinSound, set Stream to coin.wav, and set Bus to SFX.
  2. Add a CPUParticles2D as a child of Coin and name it Sparkle. Particles are a way of emitting lots of tiny specks. Set them up in the Inspector like this:
PropertyValueMeaning
EmittingOffDon't emit at first (code turns it on when the coin is collected)
Amount20Number of specks
Time → Lifetime0.5Seconds until each speck disappears
Time → One ShotOnEmit once and stop
Time → Explosiveness1Emit all specks at once (a quick burst)
Direction → Spread180Fly out in every direction
Gravity0, 300Fall a little
Initial Velocity → Velocity Min / Max150 / 260How fast they fly out
Scale → Scale Amount Min / Max5 / 9Size of each speck (pixels)
Color → Color RampNew GradientBright yellow on the left end, transparent orange on the right end

Color Ramp sets how a speck's color changes from when it's born until it disappears. If you set A (alpha, the transparency) of the right-end color to 0, the specks gradually fade out. When you're done, turn Emitting on for a moment to check how the particles look right in the editor (turn it back off once you've checked).

coin.tscn open with Sparkle (CPUParticles2D) selected. The Inspector shows Amount 20, Lifetime 0.5, One Shot on, Explosiveness 1.0, Spread 180, Gravity 0 and 300, Velocity Min 150 and Max 260, and Scale Amount 5 and 9
The Sparkle (CPUParticles2D) settings. Leave Emitting off

There are two kinds of particles: CPUParticles2D and GPUParticles2D. The GPU version is faster when you have thousands of specks, but at this scale either one works the same. The CPU version behaves the same on any computer or browser, so that's what we'll use here. For more details, see The complete guide to particles in Godot 4.

Swell and vanish with a Tween

A Tween is a tool that lets you write "change this value to that, over this much time" in one line. You get smooth motion without writing code in _process that nudges a value a little every frame.

The basic shape of a Tween
var tween: Tween = create_tween()
tween.tween_property(node_to_animate, "property_name", final_value, seconds)

Here's the whole coin.gd. What changed is inside _on_body_entered.

coin.gd
extends Area2D

# Signal emitted when the coin is collected (a custom signal we made)
signal collected

# How far (in pixels) and how fast the coin bobs
@export var float_height: float = 8.0
@export var float_speed: float = 3.0

var time: float = 0.0
var start_y: float = 0.0


func _ready() -> void:
	start_y = position.y


func _process(delta: float) -> void:
	time += delta
	# sin() glides smoothly between -1 and 1, which makes the coin bob up and down
	position.y = start_y + sin(time * float_speed) * float_height


# Called when the player touches the coin (connected in the editor's "Signals" tab)
func _on_body_entered(_body: Node2D) -> void:
	collected.emit()
	# Stop watching so the coin can't be collected twice (we're mid-physics, so set_deferred changes it a moment later)
	set_deferred("monitoring", false)
	# Play the sound and fire the sparkles once
	$CoinSound.play()
	$Sparkle.emitting = true
	# Grow the picture while fading it out. set_parallel(true) runs both at the same time
	var tween: Tween = create_tween()
	tween.set_parallel(true)
	tween.tween_property($Sprite2D, "scale", Vector2(1.8, 1.8), 0.25)
	tween.tween_property($Sprite2D, "modulate:a", 0.0, 0.25)
	# Deleting right away would cut off the sound and sparkles too, so wait for them to finish first
	await get_tree().create_timer(0.6).timeout
	queue_free()
  • set_deferred("monitoring", false) stops the coin from watching for "was I touched?" This keeps it from being collected again, adding to the score twice, during the 0.6 seconds before it disappears. As in Lesson 7, we're in the middle of physics processing, so we use set_deferred (change it a moment later).
  • "modulate:a" means "only the a (alpha, transparency) of modulate (the color)." Setting it to 0 makes it fully transparent.
  • A Tween normally runs its steps one at a time, in the order you write them. Calling set_parallel(true) makes everything written after it run at the same time. Here, "swell" and "fade" happen together.
  • What we animate is not Coin but its child Sprite2D. If we made the whole Coin transparent, its child Sparkle would become transparent too.
  • await get_tree().create_timer(0.6).timeout means "wait 0.6 seconds." The function pauses at the await line, and when the time is up, the following queue_free() runs.

BGM, and a pause after the game ends

  1. Add an AudioStreamPlayer as a child of Main in main.tscn and name it BGM. Set Stream to bgm.wav and Bus to BGM, and turn on Autoplay.

With Autoplay on, it starts playing automatically when the scene starts. We only put it in the game scene, so it doesn't play on the title screen or the result screen.

Next, update main.gd. We'll add flash_label(), which makes a label glow when the score or lives change, plus code that fades the BGM down and waits 1 second when the game ends.

main.gd
extends Node2D

# Number of coins on the stage (counted in _ready)
var coin_total: int = 0
# Time since the start (seconds)
var time: float = 0.0
# Set to true when the game ends (after that, we stop counting time and hits)
var is_finished: bool = false

# The HUD labels. With @onready, they get filled in right before _ready
@onready var score_label: Label = $HUD/TopBar/Row/ScoreLabel
@onready var time_label: Label = $HUD/TopBar/Row/TimeLabel
@onready var lives_label: Label = $HUD/TopBar/Row/LivesLabel


func _ready() -> void:
	# We're starting a new game, so reset the score and lives to their starting values
	GameState.reset()
	var coins: Array[Node] = get_tree().get_nodes_in_group("coins")
	coin_total = coins.size()
	# In code, connect the collected signal of every coin in the "coins" group
	for coin: Node in coins:
		coin.connect("collected", _on_coin_collected)
	# Also connect, in code, the hit signal the player emits when it touches an enemy
	$Player.connect("hit", _on_player_hit)
	update_hud()


func _process(delta: float) -> void:
	if is_finished:
		return
	time += delta
	# %.1f is replaced by "the number with one decimal place"
	time_label.text = "Time: %.1f" % time


# Called when any coin emits its collected signal
func _on_coin_collected() -> void:
	GameState.score += 1
	update_hud()
	flash_label(score_label)
	# Collecting every coin clears the stage
	if GameState.score == coin_total:
		end_game(true)


# Called when the player emits its hit signal
func _on_player_hit() -> void:
	if is_finished:
		return
	GameState.lives -= 1
	update_hud()
	flash_label(lives_label)
	# When lives reach 0, it's game over
	if GameState.lives <= 0:
		end_game(false)


func update_hud() -> void:
	# %d is replaced by a whole number. For two or more, pass them in a [ ] list
	score_label.text = "Coins: %d / %d" % [GameState.score, coin_total]
	lives_label.text = "Lives: " + str(GameState.lives)


# Turn the label yellow for an instant, then fade it back to white over 0.4 seconds
func flash_label(label: Label) -> void:
	label.modulate = Color(1.0, 0.85, 0.2)
	var tween: Tween = create_tween()
	tween.tween_property(label, "modulate", Color.WHITE, 0.4)


# End the game and switch to the result screen
func end_game(is_clear: bool) -> void:
	# If the game has already ended, do nothing (so it's safe to call twice)
	if is_finished:
		return
	is_finished = true
	GameState.finish(is_clear, time)
	# Lower the BGM volume over 1 second (-40 dB is barely audible)
	var tween: Tween = create_tween()
	tween.tween_property($BGM, "volume_db", -40.0, 1.0)
	# Don't switch right away: wait 1 second to let the moment land. Sound effects and effects finish meanwhile
	await get_tree().create_timer(1.0).timeout
	get_tree().change_scene_to_file("res://result.tscn")
  • flash_label() is a Tween for UI. It instantly turns the label's modulate (the color multiplied over it) yellow, then brings it back to white over 0.4 seconds. Your eye is drawn to "the number changed."
  • Tweens can animate more than position and size. Any numeric property works, like $BGM's volume_db.
  • In Lesson 7, we switched to the result screen the instant you grabbed the last coin. That cuts off the coin's sound and sparkles partway through. By waiting 1 second with await before switching, you get to see the effects finish before moving on. When the timer's wait ends, we're no longer in the middle of physics processing, so call_deferred isn't needed anymore.
  • The game keeps running during that 1-second wait, so is_finished makes sure that after the end, "the timer stops," "touching an enemy doesn't cost a life," and "end_game doesn't run twice."

Squash on landing, shake on hit

Last up is the player. We'll squash the picture for an instant the moment it lands, and give the camera a small shake when it touches an enemy. Here's the whole player.gd:

player.gd
extends CharacterBody2D

# Signal emitted when we touch an enemy
signal hit

# Left/right movement speed (pixels per second)
@export var speed: float = 300.0

# Gravity strength: the downward speed increases by this much every second
@export var gravity: float = 1200.0

# Speed at the moment of the jump. Up on screen is negative, so this is a negative value
@export var jump_velocity: float = -600.0

# Starting position (we come back here after touching an enemy)
var start_position: Vector2 = Vector2.ZERO


func _ready() -> void:
	start_position = position


# Called on every physics step (60 times per second by default)
func _physics_process(delta: float) -> void:
	# When not standing on the floor, increase the downward speed a little at a time (this is gravity)
	if not is_on_floor():
		velocity.y += gravity * delta

	# The moment the jump button is pressed, jump if we're on the floor
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
		$JumpSound.play()

	# If the button is released while still rising, halve the upward speed (short hop)
	if Input.is_action_just_released("jump") and velocity.y < 0.0:
		velocity.y *= 0.5

	# -1 for left, 1 for right, 0 if neither is pressed
	var direction: float = Input.get_axis("move_left", "move_right")
	velocity.x = direction * speed

	# Before moving, remember whether we were on the floor
	var was_on_floor: bool = is_on_floor()

	# Move according to velocity. If we hit a floor or wall, stop there
	move_and_slide()

	# In the air a moment ago, on the floor now = the moment we landed
	if is_on_floor() and not was_on_floor:
		squash()


# On landing, squash the picture for an instant, then restore it
func squash() -> void:
	var tween: Tween = create_tween()
	tween.tween_property($Sprite2D, "scale", Vector2(0.6, 0.4), 0.06)
	tween.tween_property($Sprite2D, "scale", Vector2(0.5, 0.5), 0.12)


# Send the player back to the start. Called by the enemy (enemy.gd)
func respawn() -> void:
	# Announce that we got hit (taking away a life is main.gd's job)
	hit.emit()
	$HitSound.play()
	shake_camera()
	position = start_position
	velocity = Vector2.ZERO


# Shake the camera in quick little jolts by tweening Camera2D's offset (how far the view is shifted)
func shake_camera() -> void:
	var tween: Tween = create_tween()
	for _i in 6:
		var shake: Vector2 = Vector2(randf_range(-10.0, 10.0), randf_range(-10.0, 10.0))
		tween.tween_property($Camera2D, "offset", shake, 0.04)
	tween.tween_property($Camera2D, "offset", Vector2.ZERO, 0.04)
  • How to detect landing: before move_and_slide(), remember in was_on_floor whether we were on the floor; if, after moving, we're "on the floor now," that single frame is the moment of landing.
  • The Tween in squash() doesn't use set_parallel, so its steps run in order. In 0.06 seconds it becomes a wide, short shape (0.6, 0.4), then over the next 0.12 seconds it returns to the original 0.5. The player's picture is displayed at Scale 0.5 (half size), so that's the value we return to.
  • shake_camera() chains six "shift to a random position" steps into a Tween, then returns to 0 at the end. offset is "how far the camera's view is shifted," so it doesn't affect the player's position. for _i in 6 means "repeat 6 times," and the _ in _i is the same "this variable isn't used" mark as in Lesson 6.

Run it with F5. The BGM plays, jumping makes a sound, and the character squishes when it lands. Grab a coin for a sound and sparkles; touch an enemy for a sound and a screen shake. When you grab the last coin, the BGM fades down smoothly, and the clear screen appears 1 second later.

Try it
  • Remove the effects one at a time (put a # at the start of the line to turn it into a comment) and compare how the game plays. The ones you miss when they're gone are the effects that matter most for this game.
  • Change Vector2(0.6, 0.4) in squash() to Vector2(0.7, 0.3) and the character looks even softer. On the other hand, if it squashes too much, it stops looking like a solid object.
  • In the Audio panel, press the "M" (mute) button on the BGM bus. Only the BGM goes silent, and the sound effects remain. That's the payoff of splitting them into buses.
  • Add .set_trans(Tween.TRANS_BACK) after tween_property and you get a bouncy motion that overshoots slightly and comes back. Try it on the coin's swell and see the difference.

Common pitfalls

SymptomCause and fix
No sound at allThe AudioStreamPlayer's Stream is empty, or the code never reaches the line that calls play(). Also check that the bus isn't muted in the Audio panel, and check your computer's own volume.
The coin sound is barely audible, or no sparkles appearIf queue_free() comes before await, the whole coin disappears the moment the effects start. Make queue_free() the last line.
You grab one coin but the score goes up by twoWithout set_deferred("monitoring", false), the player can touch it again before it disappears.
The sparkles disappear along with the coinThe Tween is animating the modulate of Coin (itself). That makes the child Sparkle transparent too, so animate $Sprite2D instead.
Lowering the BGM bus volume doesn't change how loud the BGM isThe BGM node's Bus is still Master, or the bus name is misspelled (it's case-sensitive). If you specify a bus name that doesn't exist, the sound goes to Master.
The BGM stops after playing onceCheck that you set Loop Mode to Forward in the import settings and clicked Reimport.
You hear two copies of the BGM overlappingAutoplay is on, and you're also calling play() in code. Use only one of them.
The character stays small after every landingIf the last value in squash() is something like Vector2(1, 1), it won't match the original size (0.5). Return it to the same value as the Sprite2D's Scale.

Working with AI

If you hand this lesson's work to AI

Effects are easier to ask for when you put into words "which event, what reaction, over how much time." Handing over the table from the start of this lesson as is works well too. You can let AI pick the numbers, but in the end, you decide by playing.

This is Godot 4.6. In coin.gd's _on_body_entered, don't call queue_free() right away. After emitting collected, set monitoring to false with set_deferred, play the child CoinSound (AudioStreamPlayer), and set the child Sparkle (CPUParticles2D, One Shot) to emitting = true. Use a Tween to scale $Sprite2D up 1.8x and fade it to transparent over 0.25 seconds, then wait 0.6 seconds and call queue_free().
In player.gd, add a squash() that, at the moment of landing (in the air before move_and_slide, on the floor after), uses a Tween to squash the Sprite2D to (0.6, 0.4) and return it to (0.5, 0.5) over 0.12 seconds. In respawn(), also add a screen shake that uses a Tween to shift the Camera2D's offset randomly 6 times and then return it to 0.
  • A (chat): it's good at writing Tweens and particles and at suggesting ballpark numbers. It can't create sound files or check how things actually sound and look, so you add and tune the nodes yourself.
  • B (Claude Code, etc.): it can write scripts and .tscn files, so it can go as far as adding the AudioStreamPlayer and CPUParticles2D nodes. You can even have it generate WAV files by computing waveforms in Python, like the sound effects in this series. But the AI can't tell how the sounds sound or how the effects look.
  • C (+ MCP): with Godot MCP Pro, the AI can add audio buses (add_audio_bus), add sound nodes (add_audio_player), and create particles and apply presets (create_particles, apply_particle_preset) inside the editor. If you run the game and take a series of screenshots (capture_frames), you can check the Tween and particle motion frame by frame. Even so, it can't hear whether the sounds feel good, so that's where your ears come in.

Whichever approach you use, play it yourself at the end to check. Game feel isn't decided by numbers; it's decided by how it feels to the person playing.

Recap and what's next

  • Game feel improves when you "respond to the player's actions immediately and clearly"
  • For sound, set the file in AudioStreamPlayer's Stream and call play(). For BGM, use Autoplay and the import setting Loop Mode
  • Splitting sounds into audio buses (SFX, BGM) lets you change the volume per type
  • create_tween() and tween_property() smoothly change position, size, color, and volume
  • CPUParticles2D's One Shot and Explosiveness give you sparkles that burst just once
  • For a node that disappears, "play the effects, then delete it": wait with await, then queue_free()

Now it's a complete game with both visuals and sound. Next time, we'll let AI playtest the game. We'll run the game, send input, look at the screen, and fix things, going around that loop together with AI.

Want to check your game's effects together with AI?

With Godot MCP Pro, Claude Code or Cursor can set up audio buses and particles inside the editor, run the game, and check the motion with frame-by-frame screenshots (190 tools, $15 one-time purchase). You can finish this series without MCP.

Learn more about Godot MCP Pro · The complete guide to particles in Godot 4

All lessons in the series (11)
  1. Lesson 1: Installing Godot and setting up AI tools
  2. Lesson 2: Nodes and scenes
  3. Lesson 3: GDScript basics
  4. Lesson 4: Input and player movement
  5. Lesson 5: Gravity, jumping, and tilemaps
  6. Lesson 6: Signals, coins, and enemies
  7. Lesson 7: UI and screen transitions
  8. Lesson 8: Sound and game feelYou are here
  9. Lesson 9: Letting AI playtest your game
  10. Lesson 10: Exporting and publishing
  11. Lesson 11: Bonus: one-prompt games by genre