DEV505 / CODE RECIPE
Follow a moving sound source.
Start a looping buzz once, move its Transform, and adjust its pitch without replacing the play.
Set up the scene
Section titled “Set up the scene”Start with Your first sound if you have not set up a sound scene before. Use a separate test scene with one enabled Sound Manager, generated Sound Keys, and one Audio Listener near the sound object. Add Bee from 03-Bee to the manager's Direct events list; these are the supplied .soundgraph assets under Assets/DEV505/SoundManager/Samples.
Save the complete code below as MovingBeeSound.cs in Assets/Scripts. Wait for Unity to compile it, create an empty GameObject, and drag the script onto it. The filename must match the class name. Test one recipe component at a time so another demo controller does not compete for the same events.
Optionally create a second GameObject named Bee and drag its Transform into the Bee field. If left empty, the script follows its own GameObject. Place it about one or two units from the listener.
In Play Mode, wait until the Sound Manager is ready. To run a listed test action, open the component's three-dot menu in the Inspector and choose that action. These are the script's ContextMenu methods. Your own code or a Unity UI Button can call its public methods too. Inspector edits made during Play Mode are temporary.
Try the component
Section titled “Try the component”The script starts buzzing when Play Mode begins. Move the Bee object from left to right in the Scene view and listen to the change. Change Wing Speed in the Inspector, then choose Stop buzzing. Choose Begin buzzing to start again. Disable the component to check that it cleans up.
This small script does not animate a bee. Move the Transform yourself or attach your own movement script. Keep it within the event’s 0.8–4 unit distance range for an easy listening test.
Position and lifetime
Section titled “Position and lifetime”Following(target, owner) keeps the sound at a moving Transform. The runtime updates the voice positions as that Transform moves. At(target.position) would instead copy one position.
The owner is the hosting GameObject, which can differ from the Bee target. This lets owner-wide cleanup reach the interaction, while the sound follows the right emitter. The supplied Bee event uses owner-loss Stop: losing its required owner or follow target stops the play. See Stop versus Detach for a different policy.
Start once, update the value
Section titled “Start once, update the value”BeginBuzzing ignores repeat starts while the retained handle is alive. Update changes WingSpeed on that play. OnDisable stops it and clears the field. IsAlive asks the service whether a play still exists; a nonempty handle alone is not evidence that it is still running.
WingSpeed uses zero smoothing in this graph, so its speed/pitch change is immediate. The live parameter recipe demonstrates gradual changes instead.
Complete component
Section titled “Complete component”using DEV505.SoundManager;using UnityEngine;
public sealed class MovingBeeSound : MonoBehaviour{ [SerializeField] private Transform bee = null; [SerializeField, Range(0.7f, 1.5f)] private float wingSpeed = 1f; private SoundHandle buzzing;
private void Start() => BeginBuzzing();
[ContextMenu("Begin buzzing")] public void BeginBuzzing() { if (!Application.isPlaying || !isActiveAndEnabled || SoundBus.IsAlive(buzzing)) return;
var service = SoundServices.Current; if (service == null) { Debug.LogWarning("Add an active Sound Manager to the scene.", this); return; }
var target = bee != null ? bee : transform; var owner = service.OwnerOf(gameObject); var context = SoundPlayContext.Following(target, owner) .With(SoundParameters.Bee.WingSpeed, wingSpeed); var result = SoundBus.Play(Sounds.Bee, context); if (result.Accepted) buzzing = result.Handle; else Debug.LogWarning($"Could not play Bee: {result.Status}", this); }
private void Update() { if (SoundBus.IsAlive(buzzing)) SoundBus.SetFloat(buzzing, SoundParameters.Bee.WingSpeed, wingSpeed); }
[ContextMenu("Stop buzzing")] public void StopBuzzing() { if (SoundBus.IsAlive(buzzing)) SoundBus.Stop(buzzing); buzzing = default; }
private void OnDisable() => StopBuzzing();}Keep exploring
Section titled “Keep exploring”Read the demo walkthrough for the graph, parameters, controls, and Unity screenshots. The related API reference explains the calls used here. Return to all twelve recipes to choose another interaction.
This page imports the checked C# file directly. It was compiled against the documented Unity revision; its runtime checks and source hash are recorded in the documentation repository’s validation files.