I didn’t experienced it, but i have seen a lot of users complain about removing the PlayOneShot. As far as i know you used it to play a sound only one time. I want to make a sound be played ONE TIME, wait a few seconds and then play again in a loop: play,wait,play,wait,play,wait, etc…
But how do i do that? I made this for now but it doesn’t seems to work:
#pragma strict
var Enemy : Transform;
var distance = 150;
var Signal : AudioSource;
private var Close = false;
var timer : float; //The time it was supposed to wait.
function Update()
{
if(Vector3.Distance(transform.position, Enemy.position) < distance)
{
Close = true;
Signal.Play(timer); //Doesn't wait.
//Can't add yield wait for seconds here because it's in an Update function.
}
else
{
Close = false;
}
}
Use AudioSource.isPlaying to detect if playing is done then start Coroutine and again AudioSource.Play()
I hope this code i made special for you will explain you how to do this.
using UnityEngine;
using System.Collections;
public class AudioS : MonoBehaviour {
bool PlaySound;
// Use this for initialization
void Start () {
PlaySound = true;
}
// Update is called once per frame
void Update () {
if (PlaySound == true)
{
GetComponent<AudioSource>().Play();
PlaySound = false;
}
if(GetComponent<AudioSource>().isPlaying == false)
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2.0f);
PlaySound = true;
}
}
public class vulgerstalAudioLoopDelayed : MonoBehaviour {
public float delay;
float delayTemp;
// Use this for initialization
void Start () {
delayTemp = delay;
}
// Update is called once per frame
void Update () {
if (GetComponent<AudioSource>().isPlaying)
delay = delayTemp;
else
delay -= Time.deltaTime;
if (delay <= 0)
GetComponent<AudioSource>().Play();
}
}