- Home /
Start ambient audio tracks at random points?
I was looking for a way to initialize my ambient tracks at random points so that when each scene loads they don't sound exactly the same every time.
I searched all over but couldn't find any similar questions to this problem on unity answers... but then I eventually figured it out on my own. Very simply once I understood how to use audio.time. Here is my code; Please give alternative solutions if you they are better
Answer by Wesww · Aug 03, 2011 at 07:55 PM
var soundToPlay : AudioSource;
var randomStartingTime;
function Start(){
randomStartingTime = Random.Range(0.0, 780.0);
PlaySound();
}
function PlaySound(){
soundToPlay.time = randomStartingTime;
soundToPlay.Play();
}
function Update(){
//print (soundToPlay.time);
}
Answer by Wesww · Jun 25, 2012 at 04:46 PM
You can also just mute tracks instead of stopping them, although I wouldn't do this with a very large amount of tracks for performance reasons.
Your answer
Follow this Question
Related Questions
Random, 3d sound. 1 Answer
Why is my random sound java script not working? 2 Answers
Random footsteps 5 Answers
Sound playing at random. (JS) 2 Answers