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?