- Home /
Is it possible to make a timer that ignores timescale?
My game sets the timeScale to 0 in between waves so that the player can upgrade weapons and barriers in menus without the next wave starting. However, I also have background music playing that has an intro. I created a second audio clip that does not have an intro which i have set up to start once the timer reaches a certain time. The menu, and subsequently the timeScale freeze, are effecting the timer that tells the audio manager when to play the second clip. Is there a way to make this timer work independently of the timeScale?
public float timer;
public Sound[] sounds;
// Start is called before the first frame update
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
void Start()
{
Play("Theme");
timer = 0f;
}
public void Update()
{
timer += Time.deltaTime;
if (timer >= 240f)
{
Play("Theme 2");
}
}
public void Play (string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.LogWarning("Sound" + name + "not found!");
return;
}
s.source.Play();
}
This is the code for reference.
have you tried using an async method? something like
async void StartTimer()
{
Debug.Log("Waiting 240 second...");
await Task.Delay(TimeSpan.FromSeconds(240));
Play("Theme 2");
}
Have you looked into the System.Timer class? I use it whenever I need a timer that doesn't rely on Unity, plus its super accurate because timers are threaded. You can also tie a callback into it like xxmariofer suggested.
EDIT: Sorry I forgot, System.Timers.Timer is just a timer, which while it's useful, if you want something that runs its own update, you'll want System.Threading.Timer that's the one that allows callbacks.
Answer by unity_J016ZU_VZQzYAg · Apr 08, 2019 at 03:06 PM
Would Time.unscaledDeltaTime
work for you ?
Unlike deltaTime this value is not affected by timeScale.
it definitely made the timer work. I appreciate it!
The code I provided makes the second clip play every frame creating this glitch stutter sound. i swapped out the >= for a == and it seems to not try to play it at all. Any suggestions?
Switch back to >=
and set a boolean after playing the sound for the first time and return after every other frame if the sound is already playing (boolean true)
That definitely fixed the problem. Just had to play around with the loop point until it transitioned seamlessly. Thank you so much for your help! You rock!
Answer by coolamigo · Apr 11, 2020 at 01:49 PM
Another option that many might find useful is WaitForSecondsRealtime. Please check that out
I just logged in to say Thank You!!!
I was trying to figure out how to get a coroutine to work with timescale.
Your answer
Follow this Question
Related Questions
Working with Time in Unity 0 Answers
How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers
Timescale issues 0 Answers
How do I implement a timer into this script that will record the total time the player has played. 2 Answers
Countdown Timer 1 Answer