3D SoundFX

How can i make the explosion and hit sfx to be heard in a 3D space, where the hit happens, not on the player like the music player?

Set the spatial blend on the AudioSource to 3D.

image

Then, when you play the clip, move the AudioSource to the location of the explosion

_audioSource.transform.position = explosion.position;
_audioSource.Play();

You would obviously need an AudioSource specifically for this.


Edit
Something I’ve seen (and used) before is to create an AudioManager that will pre-create a couple of sources and re-use them if it can. It will also create more if the need arises

public class AudioManager : MonoBehaviour
{
    // How many sources should initially be available to use
    [SerializeField] int initialSourceCount = 5;

    // Constant for the spatial blend
    private const float SPATIAL_BLEND = 0.75f;

    // A queue to hold the audio sources
    private Queue<AudioSource> audioSources;

    private void Awake()
    {
        // Initialise the audio sources queue
        InitialiseAudioSources();
    }

    // Play a clip at a location
    public void PlaySFX(AudioClip clip, Vector3 location)
    {
        AudioSource = source;
        // Check if we have sources available
        if (audioSources.Count == 0)
        {
            // If there are none, create a new one
            source = CreateAudioSource($"sfxSource{initialSourceCount++:00}", SPATIAL_BLEND, false);
        }
        else
        {
            // else, get one from the queue
            source = audioSources.Dequeue();
        }

        // set the location
        source.transform.position = location;
        // set the clip
        source.clip = clip;
        // play the clip
        source.Play();

        // put the source back in the queue
        audioSources.Enqueue(source);
    }

    // Initialise the sources queue
    private void InitialiseAudioSources()
    {
        audioSources = new Queue<AudioSource>();
        for (var i = 0; i < initialSourceCount; i++)
        {
            // Create and add a source to the queue
            audioSources.Enqueue(CreateAudioSource($"sfxSource{i:00}", SPATIAL_BLEND, false));
        }
    }

    // Create a source
    private AudioSource CreateAudioSource(string name, float spatialBlend, bool loop)
    {
        // Game object with an audio source
        var audioSource = new GameObject(name).AddComponent<AudioSource>();
        // set the spatial blend
        audioSource.spatialBlend = spatialBlend;
        // set looping
        audioSource.loop = loop;
        // return
        return audioSource;
    }
}

Another Edit
Be aware that the 3D-ness of the sound is based on where the AudioListener is in relation to the AudioSource. This isn’t usually much of a problem because the camera has an AudioListener on it by default - which makes sense because that is the player’s ‘point of perception’ into the game - but if you move it somewhere else for some reason, it will affect the sound

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms