HI there, I’m trying to create a scattered sound script to emulate the scattered sound in FMOD. I tought this script would work, but nothing happens. Can anyone spot the mistake or suggest a better way to script it?
Thanks.
using UnityEngine;
using System.Collections;
public class JKScatteredSound : MonoBehaviour {
public AudioSource scatteredSound;
public AudioClip[] audioClips;
public float lowPitchRange = .95f;
public float highPitchRange = 1.05f;
public float minDelay = 1.5f;
public float maxDelay = 2.5f;
void update()
{
if(scatteredSound.isPlaying) return;
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
scatteredSound.pitch = randomPitch;
int randomIndex = Random.Range(0, audioClips.Length);
scatteredSound.clip = audioClips[randomIndex];
float randomDelay = Random.Range(minDelay, maxDelay);
scatteredSound.PlayDelayed (randomDelay);
}
}
you should probably not rely on the isPlaying flag to prevent running the logic. I’m guessing that either your sound is always playing so this never runs at all or when it runs it always runs, overriding instructions from the previous frame. I don’t think the isPlaying flag is true during the delay before playing the sound.
Make sure your sound isn’t set to loop and try something like this:
It actually works, I was tired and I didn’t see that I didn’t capitalize Update and I was going crazy ahahaha. _met44 I see the advantages of using your method I’m going to try it
So I set both minDelay and maxDelay to 10 sec. isPlaying seems to be true even during the the delay offset. Is there any other reason why I should not relay on IsPlaying? Also considering I wanna add a layer of random audio to every looping sound in the scene, is Update the best function to use, or can be too resource intensive ?
Regarding the isPlaying flag, not that I know of but I’m far from being the biggest sound expert arround.
Regarding performance, I think you’ll run into performance issue from your sounds long before that component’s update has any significant impact. If you get down to it anyways the way arround is simple, have a list of all your active sound objects and use a single component as a global manager to run this logic.