- Home /
Play audio when game object clicked
I am trying to figure out how to play an audio source when our player is clicked on. It is an open world action adventure game. Can anybody suggest the best possible way to do something like this? I do NOT have a lot of programming experience, so please be nice! Thank you!!
Answer by S33HUNT · Jul 15, 2011 at 06:15 PM
the last answer is a good one but here are a few specifics that you will likely want to understand:
OnMouseDown
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
gameObject.audio
http://unity3d.com/support/documentation/ScriptReference/GameObject-audio.html
also check out AudioSource
http://unity3d.com/support/documentation/ScriptReference/AudioSource.html
Thank you for pointing me in the right direction. For anybody else looking for the solution, here is the very simple script I attached to my character.
var SoundSource : GameObject;
function On$$anonymous$$ouseDown () { SoundSource.audio.Play(); }
This script simply calls upon the gameobject's audio source when the mouse is pressed on the gameobject. Very easy stuff!!
Answer by almo · Jul 15, 2011 at 05:42 PM
This has simple steps you can look up.
1) How to play a sound. 2) How to find out if an object is clicked.
So you just need a scipt on the object to respond when clicked, and then play audio inside there.