Pepper.SFX
All Guidance pages

Guidance / Cookbook

3D Spatial Audio

Recipe: Looping Campfire Sound

📋 What: 3D looping ambient sound attached to object

🎯 Use Case: Fire, machines, environmental loops

Asset Setup

1. Create Container:

  • Random Container: “Ambience_Campfire_RnC”
  • Audio Clips: 2-3 fire crackling loops
  • Avoid Repeat Last: 1
  • Loop: ✓
  • Volume: 0.7
  • Is3D: ✓
  • Min Distance: 2
  • Max Distance: 20
  • Rolloff: Logarithmic

2. Create Event:

  • Event: “Play_Ambience_Campfire”
  • Priority: Low
  • Max Instances: 10
  • Actions: Play Ambience_Campfire_RnC → Ambience_Bus

Code

using UnityEngine;
using AudioSystem;

public class CampfireAudio : MonoBehaviour
{
    [SerializeField] private AudioEvent fireEvent;
    [SerializeField] private bool playOnStart = true;

    private AudioHandle fireHandle;

    void Start()
    {
        if (playOnStart)
        {
            StartFire();
        }
    }

    void StartFire()
    {
        if (fireEvent != null)
        {
            fireHandle = fireEvent.Post(gameObject, transform.position);
        }
    }

    void StopFire(float fadeTime = 1f)
    {
        if (fireHandle != null)
        {
            fireHandle.Stop(fadeTime);
            fireHandle = null;
        }
    }

    // Optional: Adjust volume based on fire intensity
    public void SetIntensity(float intensity)
    {
        if (fireHandle != null)
        {
            fireHandle.SetVolumeScale(intensity);
        }
    }

    void OnDisable()
    {
        StopFire(0.5f);
    }

    void OnDestroy()
    {
        StopFire(0.2f);
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, 2f);   // Min distance
        Gizmos.color = new Color(1, 0, 0, 0.3f);
        Gizmos.DrawWireSphere(transform.position, 20f);  // Max distance
    }
}

Setup:

  1. Attach to campfire GameObject
  2. Assign event
  3. Sound follows object if it moves

✅ Result: Realistic 3D ambient loop!

From the Pepper.SFX 3.1 guides (USER_GUIDE.md). Something wrong or unclear? Tell me.