- Home /
touch the object to play an audio
i want to play an audio clip only when a 3d object is touched/clicked....in my project there are 6 3d objects each having its own audio file which should be played only when it is touched or clicked....i used itween to do it but it is playing automatically when it is not clicked/touched ...here comes my coding (NOTE: i have unchecked play awake button too)
var target : GameObject;
var eventName : String;
var sound1 : AudioClip;
function OnMouseDown() {
audio.clip = sound1;
audio.Play ();
iTweenEvent.GetEvent(target, eventName).Play();
}
function Update () {
}
in target placed my game object and in audio placed appropriate clip somebody save me ....
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
Answer by The-Arc-Games · Sep 18, 2012 at 12:00 PM
An approach suggestion, to make sure the event is triggering on actual mouse interaction:
use the OnMouseUp function
mouse click requires a COLLIDER component (in a layer different than IgnoreRaycast)
a kinematic rigidbody is necessary unless the collider is a TRIGGER (Is Trigger checkbox flagged)
CRITICAL: make sure that the audiosource hosted with the script is not set to 'Play On Awake' and 'Loop' (both checkboxes unmarked)
Your answer
Follow this Question
Related Questions
Play Oneshot Audio On Touch 0 Answers
audio clip to object and object zoom 0 Answers
play audio on touch 1 Answer
Play a sound when i look at a certain object? 1 Answer
audio.Play not working 1 Answer