Alright, I know this has been posted before, but for the life of me I can’t seem to get this to work. And it seems like this should be simple. I’m pretty new at this, so bear with me.
I have a door, and when my first person controller collides with it, I want a sound to occur. I have tried using an OnTriggerEnter function with a box collider, and it works, but I don’t want the sound to go off when he is going THROUGH the open door as well, only when he is colliding with the closed door.
I feel like an OnCollisionEnter would be a good bet, but it doesnt work; the sound does not go off. How come it goes off with an OnTriggerEnter and not with OnCollisionEnter? Is there some setting I am missing?
I have this script:
#pragma strict
var PressSpace : AudioClip;
function OnColliderEnter(o:Collider){
Debug.Log("The trigger fired");
audio.PlayOneShot(PressSpace);
}
I don’t know if this is the best solution, but what I would do is just set a Boolean flag on the door telling whether it is open or closed. In your OnCollisionEnter, check that value and if the door is closed, play the sound… otherwise ignore it.
Hmm. There has to be a simpler way of doing this. Is there a particular setting that needs to be enabled with OnCollisionEnter functions that I might be missing? It seems like a simple piece of code
I’ve also tried something like this, and it still doesn’t work. The soundclip has been added to the script in the inspector as well as an audio source in the inspector. I don’t know what I could be doing wrong
var PressSpace : AudioClip;
function OnCollisionEnter (collision : Collision) {
audio.PlayOneShot(PressSpace);
}
Does it work if you just call it from the start function. Also, put a debug in the OnCollisionEnter and make sure it’s being called. Double check all spelling.