Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Last active January 9, 2018 12:00
Show Gist options
  • Select an option

  • Save paulhayes/180d567fb8f27327c98159ee4ba55bc9 to your computer and use it in GitHub Desktop.

Select an option

Save paulhayes/180d567fb8f27327c98159ee4ba55bc9 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundDelayRepeat : MonoBehaviour
{
private AudioSource audioSource;
private float lastPlay;
public float firstTimeDelay;
public float timeInbetweenSound;
void Start ()
{
audioSource = GetComponent<AudioSource>();
StartCoroutine( PlayLoop() );
}
IEnumerator PlayLoop ()
{
yield return new WaitForSeconds(firstTimeDelay);
audioSource.Play();
while(true){
yield return new WaitForSeconds(timeInbetweenSound);
audioSource.Play();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundDelayRepeat : MonoBehaviour {
private AudioSource audioSource;
private float lastPlay;
public float firstTimeDelay;
public float timeInbetweenSound;
void Start ()
{
audioSource = GetComponent<AudioSource>();
lastPlay = firstTimeDelay - timeInbetweenSound;
}
void Update ()
{
if (Time.time > lastPlay + timeInbetweenSound) {
timeInbetweenSound = Time.time;
audioSource.Play();
lastPlay = Time.time;
}
}
}
@paulhayes
Copy link
Copy Markdown
Author

reworked with differing initial delay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment