- Home /
How to make sound play once until called again
So, If my player jumps and lands, I want to make a thump noise like youn would if you landed on the ground. The problem I'm having is that I can't get it to only play once, and that is when you hit the ground. This is the script so far.
#pragma strict
var isGrounded : boolean = true;
var sounds: AudioClip[];
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
isGrounded = true;
if (audio.isPlaying) return;
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
}
else
{
isGrounded = false;
}
}
I know the problem with this but I just don't know how to fix it. I can't just time the sound because if a player jumps of a hill I want it to make the sound when he lands not in mid air.
Surely you meant line 14 should be isGrounded = false...right? And it belongs right after audio.Play(); (line 17)
Please accept the answer if it has resolved your concern.
Answer by clunk47 · Sep 05, 2013 at 09:17 PM
Have a look at AudioSource.PlayOneShot. Also make sure your audio only plays if it is not already playing, by using AudioSource.isPlaying.
if(!audio.isPlaying)
{
audio.PlayOneShot(AudioClip clip, float volume);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
someone is there? 1 Answer
Make the audio manager select from an array of sounds 1 Answer
Need help making an audio trigger. 2 Answers