- Home /
how do I make a sound play when player controller hits a tagged item
lets say I have a door tagged, "door." what is the code to play a sound when controller hits the tag?
Answer by MC HALO · Nov 16, 2010 at 04:27 AM
Ok you need to make a new script and add the following code:
var DoorSound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "door") {
AudioSource.PlayClipAtPoint(DoorSound,transform.position);
}
that should do it :)
awesome! few questions tho. 1) do I add this to the door or the controller? 2) where do I put the name of my audio file? 3) will the sound play from the controller or the door? (I want the sound co$$anonymous$$g from the door so that if the controller moves away from the door the sound gets quieter)
Ok thats fine, what you have to do is add this script to the player and. Once you create the variable $$anonymous$$y audio clip it will appear on the inspector mode and you look at the script you just attached on the player and you will see it there :) and the sound will be played from the player if you want you can send me your stuff and i will happily solve it for you i will also add comments so that the code will be easier to understand and etc. if you would like me to help send me on my e-mail address
i will be looking forward to help you sgreco :)
i have done everything in this correctly and my sound wont play when my player collider hits the other collider (fountain in my case) could it be because i already have a looped sound playing from my player camera...?
No that should not be a problem :) if you send it to me i can have a look for you and fix it asap i will need the project folder sent to my google account which is :
please send the full project file not the script alone :)
Your answer
Follow this Question
Related Questions
Play sound on collision 1 Answer
How do I play a sound after the mouse goes through an object? 3 Answers
make sound work 0 Answers
repeated player collisions 1 Answer
Background Music 2 Answers