- Home /
[NoBraves] Sound makes lag
Hello everyone. I am making an explorer game with a procedurally generated terrain and some animal AI in the world. Yesterday I made my sound API for the game with some usefull methods etc. The problems is that the sound decreases a LOT the fps. Without ths sound the game has around 100 FPS and as soon as I toggle them, My game has around 20 FPS. I display sounds by spawning an empty GameObject a a given point and make him .Play() the clip. I destroy the gameObject once the sound is finished. I allready made some optimization by only playing the sound if the distance between the player and the sound is less than the max distance but it still lags.
Here is a great example of the code I've done
public enum Parameter { LOOP, TWO_DIMENTIONAL, }
public static AudioSource Source(GameObject holder) {
return holder.AddComponent<AudioSource>();
}
public static AudioClip Play(SoundEffect sound, Vector3 position) { return Play (sound, new Sound.Parameter[0], position); }
public static AudioClip Play(SoundEffect sound, Sound.Parameter[] param, Vector3 position) {
if (sound.clips.Length == 0 || GameSettings.sqrtDistance3D (PlayerController.instance.transform.position, position) > (sound.maxDistance + 4f * sound.clips[0].length)*(sound.maxDistance + 4f * sound.clips[0].length)) return null;
List<Sound.Parameter> parameters = new List<Sound.Parameter> (param);
GameObject soundPlayer = new GameObject ("SoundEffect " + sound.clips[0].name);
soundPlayer.transform.position = position;
AudioSource source = Source (soundPlayer);
source.volume = sound.volume;
source.maxDistance = sound.maxDistance;
source.loop = (parameters.Contains (Parameter.LOOP));
source.spatialBlend = (parameters.Contains(Parameter.TWO_DIMENTIONAL)) ? 0f : 1.0f;
AudioClip clip = sound.clips [Random.Range (0, sound.clips.Length)];
source.clip = clip;
source.Play ();
if (!source.loop) GameObject.Destroy (source, clip.length);
return clip;
return null;
}
So I really don't know why it does that, maybe I have too much sound effects in my world. For example the footsteps sounds are played each 0.3 second for each near animal around 30 meters (sometimes I have like 10 animals around me so there is around 30 footsteps sounds per seconds)
Otherwise I generate water sounds on my water by detecting it at the start procedurally.
So If you have anyu ideas of why it decreases so much my fps, please answer this unity question, It would be really great ! If you want to see any code just ask I will provide it, there is actually a lot of code so I can't post everything here. Thank you very much for listening at this, I hope one of you has the answer !
Julien, Head of the NoBraves Studio channel.
Answer by Reasqn · Jul 19, 2017 at 02:32 PM
Here is a gread example of sounds in the world
Your answer
Follow this Question
Related Questions
Realistic Sound Effect 0 Answers
Background Music 2 Answers
Is there a way to create a random Audiosource loop? 2 Answers
Audio not playing... 2 Answers
Audio in WEBGL on IOS devices -1 Answers