- Home /
audio.clip Messing with Other Audio Clips?
Hey guys, new to Unity, and I was wondering what the heck is wrong with my code? I have a GameObject with a Capsule Collider set up on it and have "isTrigger" checked.
Here the code that is messing up:
var sound : AudioClip;
var volume = 0.5;
var triggered = false;
function OnTriggerEnter(otherObj: Collider){
audio.clip = sound;
audio.volume = volume;
if (otherObj.tag == "Player"){
if (Input.GetKeyDown(KeyCode.E)) {
triggered = true;
}
}
if (triggered) {
sound.Play();
}
}
I also have my character that everytime he walks, a footstep sound plays, Now, I know this is not the most effiecent way to do this with the footsteps but I was in a rush to leave for work. Here is the code for that: var AudioFile : AudioClip; var Volume: float; //Code wont function properly. If two buttons are pressed at once, it starts the sound clip over again and wont play correctly.
var AudioFile : AudioClip;
var Volume: float;
//Code wont function properly. If two buttons are pressed at once, it starts the sound clip over again and wont play correctly.
function Update() {
audio.volume = Volume;
audio.loop = true;
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
if (Input.GetKeyUp(KeyCode.W))
{
audio.Pause();
}
else if (Input.GetKeyDown (KeyCode.A))
{
audio.Play();
}
else if (Input.GetKeyUp(KeyCode.A))
{
audio.Pause();
}
else if (Input.GetKeyDown (KeyCode.S))
{
audio.Play();
}
else if (Input.GetKeyUp(KeyCode.S))
{
audio.Pause();
}
else if (Input.GetKeyDown (KeyCode.D))
{
audio.Play();
}
else if (Input.GetKeyUp(KeyCode.D))
{
audio.Pause();
Now, I think the audio.clips are messing with each other, but I dont know how to fix it? I walk up into the trigger, and when I hold down "E", the sound will ONLY play cut up when I walk backwords out of the trigger?
Can anyone help?
P.S. If you guys could help with the correct efficient way to code the footsteps, that would be awesome. The code only plays when 1 key is held down, not two, or three. When two or more are held down, the footstep sound stops completely.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multi-Surface footsteps with FPSController 0 Answers
Footstep audio for FPS 1 Answer
How to add footsteps to Lerpz? 1 Answer
Random Footsteps within CollisionSoundEffects Script 1 Answer
Footsteps with iTween MoveTo 1 Answer