Skip to content
Snippets Groups Projects
Commit 03686f0e authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Update Readme, more comfort

parent e82d69d8
Branches
No related tags found
No related merge requests found
......@@ -524,6 +524,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 24c88adf6dcd2dfd7bc209f1b232bdce, type: 3}
m_Name:
m_EditorClassIdentifier:
player: {fileID: 672941564}
audioSource: {fileID: 0}
stopOnExit: 0
pauseOnExit: 1
startOnEnter: 1
......@@ -616,7 +618,7 @@ AudioSource:
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 8300000, guid: 8f96ec519bd0c194fb9d193480403943, type: 3}
m_PlayOnAwake: 1
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
Loop: 0
......@@ -625,8 +627,8 @@ AudioSource:
SpatializePostEffects: 0
Priority: 128
DopplerLevel: 1
MinDistance: 1
MaxDistance: 500
MinDistance: 0.1
MaxDistance: 2
Pan2D: 0
rolloffMode: 0
BypassEffects: 0
......
......@@ -7,7 +7,8 @@ using System;
[RequireComponent(typeof(Collider))]
public class SoundManager : MonoBehaviour
{
public GameObject player;
public Collider player;
public AudioSource audioSource;
public Boolean stopOnExit = false;
public Boolean pauseOnExit = false;
public Boolean startOnEnter = true;
......@@ -17,13 +18,17 @@ public class SoundManager : MonoBehaviour
public float stopDelay = 0.0f;
Boolean never_played_before = true;
AudioSource source;
// Start is called before the first frame update
void Awake()
{
source = GetComponent<AudioSource>();
source.playOnAwake = false;
source.Stop();
// If the audioSource is not set, use the Component of this element
if (audioSource == null)
{
audioSource = GetComponent<AudioSource>();
Debug.LogFormat("[{0}] Audio Source was not specified, so use the one placed on {1}", this.gameObject.name);
}
audioSource.playOnAwake = false;
audioSource.Stop();
if (startAfterAwake)
{
Invoke("Play", startDelay);
......@@ -32,59 +37,59 @@ public class SoundManager : MonoBehaviour
void OnTriggerEnter(Collider other)
{
if (startOnEnter && other.gameObject.name == player.name)
if (startOnEnter && other.gameObject.name == player.gameObject.name)
{
// 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);
}
}
else if (!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);
}
}
void OnTriggerExit(Collider other)
{
if (stopOnExit && other.gameObject.name == player.name)
if (stopOnExit && other.gameObject.name == player.gameObject.name)
{
Invoke("Stop", stopDelay);
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);
}
else if (pauseOnExit && other.gameObject.name == player.name)
else if (pauseOnExit && other.gameObject.name == player.gameObject.name)
{
Invoke("Pause", stopDelay);
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);
}
}
void Play()
{
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);
Data.instance.RegisterSound(audioSource.name);
}
}
void Stop()
{
source.Stop();
audioSource.Stop();
}
void Pause()
{
source.Pause();
audioSource.Pause();
}
}
......@@ -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.
## Settings explained
| Setting | Explaination |
| ------------------- | ------------------------------------------------------------ |
| `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
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment