// Start sound only if we want to "restart on enter"
// Otherwise only start it, if it isn't playing already
if(never_played_before||!source.isPlaying)
if(never_played_before||!audioSource.isPlaying)
{
Invoke("Play",startDelay);
Debug.LogFormat("[{0}] Started playing sound because of collision with {1}",this.gameObject.name,other.gameObject.name);
Debug.LogFormat("[{0}] Started playing sound {1} with a delay of {2} seconds because of collision with {3}",this.gameObject.name,audioSource.clip.name,startDelay,other.gameObject.name);
}
}
elseif(!startOnEnter)
{
Debug.LogFormat("[{0}] Collided with {1} but didn't trigger sound because it wasnt {2}",this.gameObject.name,other.gameObject.name,player.name);
Debug.LogFormat("[{0}] Collided with {1} but didn't trigger sound because it wasnt {2}",this.gameObject.name,other.gameObject.name,player.gameObject.name);
Debug.LogFormat("[{0}] Stopped sound because {1} exited collider",this.gameObject.name,other.gameObject.name);
Debug.LogFormat("[{0}] Stopped sound {1} with a delay of {2} seconds because {3} exited collider",this.gameObject.name,audioSource.clip.name,stopDelay,other.gameObject.name);
Debug.LogFormat("[{0}] Paused sound because {1} exited collider",this.gameObject.name,other.gameObject.name);
Debug.LogFormat("[{0}] Paused sound {1} with a delay of {2} seconds because {3} exited collider",this.gameObject.name,audioSource.clip.name,stopDelay,other.gameObject.name);
}
}
voidPlay()
{
if(playOnlyOnce){
if(!Data.instance.PlayedSound(source.name)){
source.Play();
if(!Data.instance.PlayedSound(audioSource.name)){
audioSource.Play();
never_played_before=false;
Data.instance.RegisterSound(source.name);
}else{Debug.LogFormat("[{0}] Didn't play sound again: {1}",this.gameObject.name,source.name);}
Data.instance.RegisterSound(audioSource.name);
}else{Debug.LogFormat("[{0}] Didn't play sound again: {1}",this.gameObject.name,audioSource.clip.name);}
}else{
source.Play();
audioSource.Play();
never_played_before=false;
Debug.LogFormat("[{0}] Registered as Played: {1}",this.gameObject.name,source.name);
Data.instance.RegisterSound(source.name);
Debug.LogFormat("[{0}] Registered as Started: {1}",this.gameObject.name,audioSource.name);
@@ -4,10 +4,30 @@ Allows dynamic playback, resuming and triggering of sounds with Trigger Volumes.
## Although the Script works, the Project-File is just a draft!
## For a Scene setup follow these steps
just
1. Add the Script `Data.cs` to an empty object in your scene that is called `Data` and enter the current Scene's name in the Data-Components field `Current Scene`
2. Add the Script `SoundManager.cs` onto any object that has a collider on it.
3. Set the collider to `Is Trigger`, this Trigger will now be automatically used by all the `SoundManager`-Components on this object.
4. Select the object with the player collider on it in the `SoundManager`-Component's settings (`Player`). This is needed so random collisions won't trigger the sound
5. Add a `Sound Source` component onto the trigger (or any other object in the Scene) and deactivate `Play On Awake` if this is not what you want.
6. Select the `Sound Source` in the `SoundManager`-Component's settings.
| `Player` | The object that should be allowed to trigger the sounds. Needs to have a Trigger-Collider on it |
| `Audio Source` | A `Audio Source`-Component that will be triggered by the collision. Can be any other object. If you want to trigger multiple sounds at once, add multiple `SoundManager`-Scripts |
| `Stop On Exit` | Stop the sound and rewind to start when the player no longer collides with the Trigger |
| `Pause on Exit` | Pause the sound when the player no longer collides with the Trigger (and resume playback once the player enters again unless `Play Only Once` is active) |
| `Start On Enter` | Start the sound when the player enters the Trigger |
| `Start after Awake` | Start the sound after the Scene is loaded (with a delay you can set yourself) |
| `Play Only Once` | Don't play the sound again after it has once been started |
| `Start Delay` | Wait for this many seconds before the sound is started (either by collision or by Awake) |
| `Stop Delay` | Continue playing the sound for this many seconds after the player left the trigger |
If you want to loop the sound, add spatial effects or similar things, use the settings on the Audio Source
1. Add the Script `Data.cs` to an empty object in your scene that is called `Data`
2. Add the Script `SoundManager.cs` onto any Trigger Volume
3. Add a Sound Clip to the (automatically added) Audio Source