- Home /
play audioclip problem
Hi there, when I raycast my object it will show me the GUIDrawTexture and when i'm not hitting the object my audioClip will play.
I need the audioClip to play when the GUIDrawTexture is shown (like at the same time). dont know what i'm doing wrong see my code
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 100))
{
if(hit.collider.gameObject.tag == "kat")
{
hitter = true;
{
Print("I am looking at" + hit.transform.tag);
audio.clip = audioKat;
audio.Play();
}
}
}
}
function OnGUI() {
if(hitter == true && hit.collider && hit.collider.gameObject.tag == "kat")
{
GUI.DrawTexture(position, katImage); //"draws" image
}
}
Try this code ....
var audiokat :AudioClip;
if(hit.collider.gameObject.tag == "kat") { AudioSource.PlayClipAtPoint( audiokat, transform.postion); }
Answer by HYPERSAVV · Dec 18, 2014 at 08:04 AM
You are telling the audio clip to start playing for each frame that the raycast hits the object. When you look away, it finally has a chance to play the object. You want the Play() method to be called once, so use audio.isPlaying() to check the status.
Note: You will need to add some other sort of flag (bool) that says it has played already, and set it to false when you look away otherwise extended periods of time will just play the sound over and over.
Your answer
Follow this Question
Related Questions
[Solved] Generating Particles with Attack Animation 1 Answer
How to create dynamic array of type GameObject using javascript? 2 Answers
accessing a variable from one script in another with Unity 1 Answer
How Do I Create A Nested Function With Unity JavaScript? 1 Answer
Adding sound javascript in unity2d 1 Answer