- Home /
Can you make this work? (Click Collider = Make Sound)
Hello, first of all thank you for taking a look at this question as it's been really bugging me.. I've spent at least 7 to 8 hours of research and have found nothing.
Basically, all I want is..
When the user clicks the Mouse Down on a Box collider that is around a button. (E.g, Door Button) I want it to play an open sound, but ONLY when the mouse is clicked on that SPECIFIC collider. The furthest I have got is, being able to make the sound by clicking, but clicking anywhere makes the sound. I want it to make the sound only when I click on the COLLIDER.
SIMPLE WAY: User Clicks on Collider >>> Sound of door opening >>> User Clicks on Collider again >> Sound of door closing.
EXAMPLE: User clicks on the Box collider around the button. (THIS SHOULD THEN MAKE A SOUND, AND THEN WHEN CLICKED ON AGAIN, IT SHOULD MAKE A SOUND AGAIN BUT WHEN THE DOOR CLOSES)
I know from my experience of a little bit of scripting that, it's around the lines if MouseClickDown is pressed again on the same object, Play.AudioIsPlaying, or something like that. Haha, you have the permission to laugh at me now as that was probably all wrong, but you know..
You want a Book for if the door is open or not.
If(doorOpen){ PlaySoundClose; //reverse the bool status doorOpen = !doorOpen; If(!doorOpen){ PlaySoundOpen; //reverse the bool status doorOpen = !doorOpen;
This would all be in another IF condition that did a ray collision check based on mouse screen position and did an if gameobject.tag == door check, which has been answered on here many times.
Answer by ICHeeryI · Jan 11, 2014 at 02:40 PM
You can use raycast: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
var once = 0;
function Update() {
if (Input.GetMouseButtonDown (0)){
var hit : RaycastHit; //Raycast hit
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 10)){
if(hit.collider.gameObject.tag == "Button"){ //You need to assign tag to object
if(once==0)once=1;//Door opens
if(once==1)once=2;//Door closes
}
}
}
else if (once==2){
//We opened and closed it reset
once=0
}
}
You can use: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html with: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Good luck!
Your answer
Follow this Question
Related Questions
Can you fix my code? (Audio for doors) 2 Answers
How do I fix audio loop delay 2 Answers
Wait for sound is played 2 Answers
Game created that Users can add own music? 1 Answer
Sound not working? 1 Answer