Lesson 11

Sound, and exporting GlitchFall

The finish line for 3D. We add positional pickup sounds, a bit of ambience, and export a playable build. By the end you have a complete GlitchFall to share.

▸ By the end of this lesson: Positional pickup audio, ambience, and an exported build of the finished game

11.1Positional pickup sound

3D games can place sound in space with AudioStreamPlayer3D — a pickup on your left sounds like it's on your left. Add one to the Fragment.

  1. Open fragment.tscn. Add a child AudioStreamPlayer3D named PickupSound. Drag a short chime .ogg/.wav into its Stream slot.

Play the sound before freeing

If you queue_free() the fragment immediately, its audio player dies before the sound finishes. Two clean options: (a) play the sound, then free after a short delay via the tween callback; or (b) detach the sound. Simplest is to reparent the sound to Main or delay the free — shown below.

Adjust the pickup so the sound survives the fragment. In fragment.gd:

fragment.gd (replace)gdscript
@onready var pickup_sound: AudioStreamPlayer3D = $PickupSound

func _play_pickup_and_free() -> void:
	set_deferred("monitoring", false)
	# Move the sound player to the parent so it outlives this node.
	if pickup_sound and pickup_sound.stream:
		pickup_sound.reparent(get_parent())
		pickup_sound.play()
		# Auto-free the reparented sound when it finishes.
		pickup_sound.finished.connect(pickup_sound.queue_free)
	var tw: Tween = create_tween()
	tw.tween_property(self, "scale", scale * 1.8, 0.15)
	tw.tween_callback(queue_free)

reparent keeps world position

reparent() moves a node to a new parent while preserving its world transform — the sound keeps playing from where the fragment was. The finished signal then cleans it up. No orphaned nodes.

11.2Ambience

  1. Add an AudioStreamPlayer (non-positional) to Main named Ambience. Assign a looping pad/drone stream.
  2. In the stream's import settings (select the file in FileSystem → Import tab), enable Loop, then re-import. Set the Ambience player's Autoplay on.

2D vs 3D audio players

AudioStreamPlayer = non-positional (music, UI, ambience). AudioStreamPlayer3D = positional (world sounds that pan and attenuate with distance). Use the right one for the job.

11.3Export a build

  1. Project → Export → Add a preset (Windows, Linux, macOS, or Web).
  2. Download export templates if prompted (first time only).
  3. For a shareable browser build, pick Web and Export Project into a folder. It produces an index.html and support files can be hosted — including on TeqStudy.
  4. Test the exported build by running it outside the editor.

Web export + mouse capture

Browsers only allow mouse capture after a user click. On web, the pointer locks when the player first clicks the canvas — that's expected behavior, not a bug. Add a 'click to start' hint for web exports.

GlitchFall complete

A lit world, a physics character, a third-person camera, collectibles, hazards with respawn, a gated exit, a HUD and win screen, glitch atmosphere, positional sound, and an export. GlitchFall is a complete 3D adventure built by example.

Checkpoint — expected state