0
0
Unityframework~5 mins

3D spatial audio in Unity

Choose your learning style9 modes available
Introduction

3D spatial audio makes sounds feel like they come from specific places around you. It helps games and apps feel more real and immersive.

You want players to hear footsteps coming from behind or beside them.
You want a sound to get quieter as the player moves away from it.
You want to create a realistic environment where sounds change based on position.
You want to guide players by making sounds come from certain directions.
You want to add depth and space to your game's audio experience.
Syntax
Unity
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.spatialBlend = 1.0f; // 3D sound
audioSource.clip = yourAudioClip;
audioSource.Play();
Set spatialBlend to 1 for full 3D sound, 0 for 2D sound.
Attach the AudioSource component to the game object that should emit sound.
Examples
Play a footstep sound that moves in 3D space with the game object.
Unity
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = footstepClip;
audioSource.spatialBlend = 1.0f;
audioSource.Play();
This plays the sound as 2D, ignoring 3D position.
Unity
audioSource.spatialBlend = 0.0f;
audioSource.Play();
Set how close or far the sound can be heard clearly.
Unity
audioSource.minDistance = 1f;
audioSource.maxDistance = 20f;
Sample Program

This script adds an AudioSource to the game object, sets the sound clip, and enables 3D spatial audio. The sound will appear to come from the object's position and get quieter as you move away.

Unity
using UnityEngine;

public class SpatialAudioExample : MonoBehaviour
{
    public AudioClip soundClip;
    private AudioSource audioSource;

    void Start()
    {
        audioSource = gameObject.AddComponent<AudioSource>();
        audioSource.clip = soundClip;
        audioSource.spatialBlend = 1.0f; // Enable 3D sound
        audioSource.minDistance = 1f;
        audioSource.maxDistance = 15f;
        audioSource.Play();
    }
}
OutputSuccess
Important Notes

Make sure your AudioListener (usually on the main camera) is active to hear 3D sounds.

Use minDistance and maxDistance to control how sound fades with distance.

Test your sounds in the Unity Editor by moving the camera or object to hear the spatial effect.

Summary

3D spatial audio makes sounds come from specific places in your game world.

Use the AudioSource component with spatialBlend = 1 to enable 3D sound.

Adjust distance settings to control how sound fades as you move around.