- Home /
Sound Repeating every frame..Want it only once!
Hey guys. I'm trying to get a sound clip to play when my cursor falls over a gameobject, but it's playing every frame. I've tried some of the suggestions around here but am struggling to bring them into my code. Please help! I've attached my code here:
using UnityEngine; using System.Collections;
public class Targeting : MonoBehaviour { public GameObject scope; // The main score that is set infront of the ship to allow aiming of ship private int Range= 70; // The range of the array private FMOD.Studio.EventInstance lazer; //variable for lazer event
// you're trying to assign the audio event to a variable so that the reddit thing makes sense
//when you leave cursor..set a variable to true that resets the if statement
void Update ()
{
RayShoot();//Stast the rayShoot array
}
void RayShoot ()
{
RaycastHit hit;
Vector3 facingDirection= transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, facingDirection * Range, Color.blue);
scope.GetComponent<Renderer>().material.color = Color.blue;
lazer = FMOD_StudioSystem.instance.GetEvent ("lazer_gun");
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Enemy") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Alliance") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.green; //Sets the scope color green
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Meteor") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
FMOD_StudioSystem.instance.PlayOneShot("event:/lazer_gun", gameObject.transform.position);
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceMothership") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.green; //Sets the scope color green
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyMothership") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemySatellite") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceSatellite") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.green; //Sets the scope color green
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AlliancePlanet") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.green;//Sets the scope color green
}
if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyPlanet") //Checks the tag
{
scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
}
}
}
Answer by Cherno · May 10, 2015 at 08:17 PM
AudioClip myClip;
bool isPlayingSound = false;
//to start the CoRoutine:
StartCoRoutine(PlaySound(myClip));
IEnumerator PlaySound(AudioClip clip) {
if(isPlayingSound == true) {
break;
}
isPlayingSound = true;
GetComponent<AudioSource>().clip = clip;
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(clip.length);
isPlayingSound = false;
yield return null;
}
Thanks for the reply! I'm having some difficulty translating this into F$$anonymous$$OD. Could you perhaps explain it a bit or just blend it with what I have? Sorry for the difficulties and thank you again.
I can't as I have no experience with F$$anonymous$$OD, but in the end you would just have to change the ...Play line so F$$anonymous$$OD plays a clip, and somehow access the clip's length so the Coroutine knows how long it should wait. The whole idea is to use bool to keep track of wether a clip is playing. A clip is only played anew if the bool is false. The CoRoutine that plays the clip will check if the bool is false, play the clip, and then wait for the clip to finish before setting the bool to false again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195257im_/https://answers.unity.com/themes/thub/images/avi.jpg)