Hello,
I have a procedural generated world. When a Player gets near a torch, I want to hear the torch.
They are not meant to play when they spawn, but play when player is near.
This is my Code:
public static void AddAudiosource (Vector3 pos, ushort voxel, Chunk chunk, AudioClip clip, float maxDist = 0)
{
try {
GameObject soundGameObject = new GameObject ("ChunkSoundAudioAmbient" + voxel);
Instantiate (soundGameObject, chunk.transform);
if (clip != null) {
soundGameObject.transform.position = pos + Vector3.up;
// Add an AudioSource component and set up some defaults
AudioSource audioSource = soundGameObject.AddComponent<AudioSource> ();
audioSource.playOnAwake = true;
audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
audioSource.minDistance = 0f;
audioSource.maxDistance = maxDist;
audioSource.spatialBlend = 1;
audioSource.rolloffMode = AudioRolloffMode.Linear;
audioSource.clip = clip;
audioSource.loop = true;
audioSource.Play();
}
}
catch (System.Exception ex) {
Debug.LogError ("In FktLight AddAudiosource Error: " + ex.ToString ());
}
}
When spawning my landscape now, I get a lot of error Messages:
“Ran out of virtual channels. Sound will not be played.”
What is the best way to add multiple environment sounds which play when near?
How can my Code be improved? Thanks!