- Home /
Unity 3.5.7 - 4.2 upgrade: Changing Time.timeScale causes OnTriggerExit & OnTriggerEnter handlers to fire
I have some code in one class to handle pausing that used to work in Unity 3.5.7. We recently upgraded our project to Unity 4.2 and we started seeing this strange behaviour.
In our scene we have a SphereCollider attached to the camera and we have some box colliders that are placed around the world with scripts on them that are reacting to the OnTriggerEnter()
and OnTriggerExit()
calls.
In our pausing code set Time.TimeScale = 0;
to pause the game. When this happens, our camera happens to be inside a BoxCollider that has a script attached to it.
In Unity 3.5.7 (and before) setting the Time.timeScale to 0 and back had no affect on OnTriggerEnter
and OnTriggerExit
handlers being fired.
In Unity 4.2 as soon as we set the timeScale to 0 the OnTriggerExit() function fires on the box collider's script. In addition when we restore the time (set it back to something non-0) the OnTriggerEnter() function fires. This is causing some nasty behaviour in our game, since we assume the camera is leaving the volume and perform some cleanup on objects in the volume. This used to be the case but now this is happening every time we pause... Not awesome.
I've tried setting the scale to something very small (0.0000001) but non-zero, but that breaks some other code we have that checks to see if the game is paused or not by examining the timeScale. I've also tried modifying the Time.fixedDeltaTime at the same time (setting it to 0 along with the Time.timeScale value), but that only seems to cause the events to fire more often.
I cant' find any reference to the expected behaviour here except that 'FixedUpdate functions will not be called when timeScale is set to zero.'
I'm not sure what to do to avoid it other than hacking in a bunch of guard code that checks to see if we're actually inside the box when the event happens. I'd need to do this in a bunch of places, so it's not exactly making me quake with joy.
Is this a unity bug or expected behaviour? Is there something I can do to make these events stop coming up when the object doesn't actually leave the box bounds?
This does sound like a bug.
Have you considered alternative pausing methods? In a project once I just did
RigidBody rb;
foreach(GameObject go in GameObject.Find(typeof(GameObject))){
rb = go.GetComponent<RigidBody>();
if(rb != null)
rb.Sleep();
//Or alternatively rb.WakeUp(); on resume
}
Though I didn't have a whole lot of GameObjects in there...
I tried to reproduce this in a super-simple project (just a box and a camera) but was unable to get it to happen. It definitely seems like a bug so I've submitted a bug report to Unity.
We are facing the same problem in here.. we manage to workaround the problem setting the timescale to a very low value. But this definetely seems to be a bug, any news from Unity about that?
just upgraded to 4.2 This is causing problems for me too. Is Unity going to fix this?
Answer by xeleh · Jul 28, 2013 at 03:30 AM
This change of behaviour is breaking my code too. I think it's a bug and as an urgent workaround I'm doing this:
private var lastTimeScale : float;
function Awake() {
lastTimeScale = Time.timeScale;
}
function OnTriggerExit(other : Collider) {
var lts : float = lastTimeScale;
lastTimeScale = Time.timeScale;
if (lts != Time.timeScale) {
return;
}
// original trigger code starts here
}
// The same for OnTriggerEnter()
It works for my case but defensive code like this is nasty... better ideas are welcome.
Your answer
Follow this Question
Related Questions
Stopping FixedUpdate 2 Answers
Orbit camer while paused? 1 Answer
Pause game on Start doesn't quite work 1 Answer
How can i pause gradually from timeScale 1.0 to 0.0 over a second? 1 Answer