title says it all really. I’m looking for something that makes a noise, then waits (user input of seconds) then plays it again. kind of like the loop, but you can change wait time after the clip plays. Thanks
Pretty easy if you don’t need it to be very accurate. The code is quite intuitive, but I have added comments.
The part where the user changes the delay is up to you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DelayedReplay : MonoBehaviour
{
private AudioSource source;
[SerializeField]
private AudioClip clip;
[SerializeField]
private float delayInSeconds = 1f;
// Start is called before the first frame update
void Start()
{
//Dynamically adding an Audiosource component
source = gameObject.AddComponent<AudioSource>();
//Dynamically setting the clip
source.clip = clip;
}
// Update is called once per frame
void Update()
{
//isPlaying is true if the audiosource is playing a clip or has a clip scheduled
if (!source.isPlaying)
{
//The audiosource is not playing a clip and it has no clip scheduled
//we need to schedule a clip to play in x seconds
source.PlayScheduled(delayInSeconds);
}
}
}
It’s more complicated if you do need it to be accurate, like you were using it in a musical setting.
You just have to schedule the audio correctly. The code will be less understandable to someone who hasn’t dabbled in scheduling audio.
Let me know if you need the more accurate version
1 Like