im making a rolling ball game. i wrote a sound script. but when the ball rollin on the floor hit sound play again n again. how can i put a limit. and disable floor sound.
var FloorSound : AudioClip;
var LockerSound : AudioClip;
var impactSoundThreshold = 0.5; // tweak this value
// both the “Enter” and the “Stay” collisions pass
// their data to the sound function:
function OnCollisionStay (collision : Collision) {
PlayCollisionSound(collision);
}
function OnCollisionEnter (collision : Collision) {
PlayCollisionSound(collision);
}
function PlayCollisionSound (collision : Collision) {
// check if the collision was hard enough to generate a sound
if (collision.relativeVelocity.magnitude > impactSoundThreshold) {
// select the correct sound effect based on the object's tag
switch (collision.gameObject.tag) {
case "Concrete": audio.clip = FloorSound; break;
case "Locker": audio.clip = LockerSound; break;
}
// calculate the volume (anything above 4*Threshold = full volume)
var volume = Mathf.InverseLerp(impactSoundThreshold, impactSoundThreshold*4, collision.relativeVelocity.magnitude);
// play the audio
audio.volume = volume;
audio.Play();
}
}