- Home /
TimeScale = 0 crashes Unity
I recently implemented a Pause menu into my game, just switching the timescale between 0 and 1.
This code causes Unity to crash:
Code:
function Update(){
if(myTransform.position == Waypoints[currentWaypoint] && currentWaypoint < 3){
currentWaypoint = currentWaypoint + 1;
Go();
}
}
function Go(){
for(t = 0; t<1;){
t += Time.deltaTime * speedMod;
myTransform.position = Vector3.Lerp(myTransform.position,Waypoints[currentWaypoint],t);
if(myTransform.position != Waypoints[currentWaypoint]){
yield;
}
}
}
I understand why it's freezing, it gets stuck in the for loop because deltaTime = 0.
So I've tried changing my pause menu to set timeScale to 0.0001. This helps, but it still gets stuck in the loop and I'm unable to unpause till the loop finishes.
I've also tried to add a break in the loop if the timeScale == 0.0001. This fixes the pausing and unpausing but causes my lerping to stop working if paused.
Can anyone give me a hand with this?
How do I handle deltaTime loops when changing the timeScale? What are my options?
I don't see any reason how this loop should prevent you from unpausing your game... It doesn't matter it the coroutine runs in the background.
Answer by Bunny83 · May 21, 2012 at 11:35 PM
Of course it does. Why do you have this if around the yield?
if(myTransform.position != Waypoints[currentWaypoint]){
yield;
}
Time.deltaTime will be 0.0 when timescale is 0.0 so this condition never gets true so your for loop loops endless without any change. Just remove the if statement and execute the yield unconditional.
edit
Hmm, actually the condition should be true, but anyway the most important thing in a coroutine is to execute yield. A lot coroutine setups uses a while(true)
loop which will never end, but it's ok as long as you call yield, every iteration.
Removing the if statement fixes the crashing. Thanks.
Now the objects speed up after the first waypoint. Something wrong with reseting t to 0 when hitting a waypoint me thinks. The if was to break loop if it reaches the waypoint. So im going to try and add break inside the if the position equals current waypoint. Also when paused the things keep moving till they reach their waypoint. Which is kinda weird.
the main problem is that you don't really lerp. The start and end position have to be constant while you lerp. Just save the current position when starting Go:
function Go(){
var startPosition = myTransform.position;
for(t = 0; t<1; t += Time.deltaTime * speed$$anonymous$$od){
myTransform.position = Vector3.Lerp(startPosition, Waypoints[currentWaypoint], t);
yield;
}
}
ha thanks alot, here I thought I was being clever with my program$$anonymous$$g. $$anonymous$$ust be funny to see what people like me come up with on here. thanks again!
Your answer
Follow this Question
Related Questions
Pause game that not using deltatime for movment 1 Answer
Pause menu... Isn't pausing everything... 1 Answer
How to Appease a pause menu in a game that changes it's time scale? 1 Answer
Accurate timing while using timeScale. 1 Answer
Single Step (pause, resume), or externally clock game loop 0 Answers