- Home /
Mute audio with GetKeyDown
What I have, is a script that starts playing a looped sound when I enter a trigger. This is a attached to a ladder, so only when the player climbs the ladder and is inside the area of the trigger, the sound plays, and mutes itself when the player leaves the trigger. However, I need the audio to mute also if the player stops climbing the ladder halfway up.
Here's what I have so far
function OnTriggerEnter (other : Collider){
if (Input.GetKey(KeyCode.W)) {
if(!audio.isPlaying) {
audio.loop = true;
audio.Play();
} if(audio.mute) {
audio.mute = false;
}
}
}
function OnTriggerExit (other : Collider){
audio.loop = false;
audio.mute = true;
}
This plays the audio while pressing "W" (the key used for climbing up) inside the trigger, and stops looping and mutes it when the player leaves. But I need it to also stop playing when the player stops pressing "W" to stop midway on the ladder, while still inside the trigger.
Does anyone know how to do something like this?
Why not use Get$$anonymous$$eyDown and Get$$anonymous$$eyUp ins$$anonymous$$d of Getkey? Also, I would consider simply using Play and Stop ins$$anonymous$$d of altering the loop and mute, but you may have other considerations in $$anonymous$$d.
Your answer
Follow this Question
Related Questions
Change view if key is pressed 4 Answers
Walking and Running Script not working. 1 Answer
Checking if a key has been pressed twice in quick succession 4 Answers
Input.GetKey question 1 Answer