- Home /
Sound playing at random. (JS)
Hey, I have a script running certain sounds when I do certain things.Besides that I want to be able to have different Soundclips playing in a random order at random times. (The game we're making is trying to be creepy so those Soundclips could be things like Screams etc.)
I would preferably want to have that in the same script to make things less complicated. Here's what I have now:
#pragma strict
var moveSound : AudioClip;
var landSound : AudioClip;
private var hasJustLanded = true;
var controller : CharacterController = GetComponent(CharacterController);
function Start () {
audio.clip = moveSound;
}
function Update()
{
var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");
if (controller.isGrounded) {
if (isMoving) {
if (!audio.isPlaying) {
audio.Play();
}
}
else if (audio.isPlaying) audio.Stop();
if (!hasJustLanded) {
hasJustLanded = true;
AudioSource.PlayClipAtPoint(landSound, transform.position);
}
}
else {
if (audio.isPlaying) audio.Stop();
hasJustLanded = false;
}
}
I appreciate any Help. Thanks in advance.
Welcome back! :D - So you just have different sound clips, that you want to play at random?
I'm glad to be back in your arms,haha :D. Yeah we're not realy sure what we want to do with it yet but I'm thinking I'll hae a Soundclip per level to play at random. I'm going to be gone for about 40 $$anonymous$$utes I'll be back.
You mean you have multiple clips that you want to choose one of them randomly and play? Or one clip that you want to play at random times?
Well it's not really my idea I'm just the one responsible for sound. but I think they mean they want different sounds to play at random times in a random order. But I think tha t one sound at random times is also possible.
you're not sure you know what you want? - I can help you if you be a bit more specific - Go ask them and make sure what exactly is required. - And make sure you add the details to your question, as it's very ambiguous as it is.
Answer by vexe · Sep 26, 2013 at 11:35 AM
1- Create an array of clips, assign its clips from the inspector.
var clips : AudioClip[];
2- Add those to what you currently have, and stick PlayRandomClip
where you want to play the sounds.
var randPercentage : float = 5; // by default, 50% chance of playing, change this to suit your needs, 0 means 0%, 1 means 10%, 8 means 80%, etc.
function PlayRandomClip()
{
var randClip = clips[Random.Range(0, clips.Length)]; // choose a clip at random
if (GetRandChanceOfPlaying())
AudioSource.PlayClipAtPoint(randClip, transform.position); // assuming that that's where you like to play the sound from
}
// This will return true 'randPercentage' % of the time
function GetRandChanceOfPlaying()
{
// just making sure randPercentage stays in [0, 10]
randPercentage = Mathf.Clamp(randPercentage, 0, 10);
return (Random.Range(0, 10) < randPercentage);
}
I'm getting comipling errors.
function //expects ( is$$anonymous$$d of PlayRandomClip & expects a semicolon
{
var randClip = clips[Random.Range(0, clips.Length-1)];
if (GetRandChanceOfPlaying())
AudioSource.PlayClipAtPoint(randClip, transform.position);
}
function GetRandChanceOfPlaying()//expects semicolon
Semicolons shouldn't be needed at the end of a variable right?
You don't need a ';' after a function declaration no. I'm not seeing where there could be an error. Although I just had a debate on Random.Range, I thought it was inclusive on both ends, it turns out it's inclusive in the beginning end, and exclusive at the right end (ONLY FOR THE INT VERSION) while in the float version both ends are inclusive, see the docs. (Very confusing and stupid in my opinion) - I updated accordingly.
am I supposed to do this in a different script? or will this work in the same one?. I guess I should've lead with that.
Answer by Sisso · Sep 26, 2013 at 11:26 AM
How to store more that one variable
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
How to get random values
http://docs.unity3d.com/Documentation/ScriptReference/Random.html
Your answer
Follow this Question
Related Questions
Why is my random sound java script not working? 2 Answers
Random Audioclip. No Repeat ?? 1 Answer
Audio not playing [Fixed] 1 Answer
Audio Needs to stop 1 Answer
Can't get a sound to loop 2 Answers