Search Unity

Make game logic timed entities independent of Unity's time

Discussion in 'Scripting' started by manuelflara, Nov 16, 2007.

  1. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    Hi,

    I'm trying to come up with a good solution to a problem I have. I want to be able to "stop the game", I mean, having the top-right countdown timer stop, have all enemies and characters stop animating, etc. while still be able to have the Unity timer running for things like do smooth fades or something like that.

    Do any one you has made something like that and is willing to share some knowledge here?

    Thanks in advance,
     
  2. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    I haven't had to do anything like this yet myself, so hopefully others will comment as well. The MonoBehaviour class has an OnApplicationPause event that gets dispatched to all your game objects. You might be able to use that to control which things stop and which do not.

    Hope this helps!
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The simplest thing is to set Time.timeScale to .0000000001 or something, and instead of using Time.deltaTime for the smooth fades, use Time.deltaTime/Time.timeScale. This isn't quite totally paused, obviously, but unless someone's planning on pausing the game and then resuming it centuries later, it probably doesn't matter.

    --Eric
     
  4. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    So for those entities I still want to be able to move when paused, I'd do that Time.deltaTime / Time.timeScale. Hmm, that's clever :)

    I'll have to test if pausing like 30 minutes (you know, someone calls and you have to answer, or something like that) makes the scene moving at timeScale 0.00000001 moves significantly or not.

    I guess I could always set it to zero, and then IF and WHEN I have to move something, set it to that low value, and after the movement reset it to zero.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, .0000000001 is a billion times slower than normal (literally), so 30 minutes pausing means about .0000018 seconds of game time actually pass, which probably isn't noticeable. :) But if it is, you can do less than .0000000001...not sure what the limit is....

    --Eric
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can write a script that uses Time.realTimeSinceStartup, and creates its own little deltaTime. realTimeSinceStartup isn't affected by timescale.

    Code (csharp):
    1.  
    2. //RealTime.js
    3. static var deltaTime : float;
    4. var previousTime : float = 0.0;
    5. function Update() {
    6. deltaTime = realTimeSinceStartup - previousTime;
    7. previousTime = Time.time;
    8. }
    9.  
    And then in your fadein/out etc scripts, use RealTime.deltaTime instead of deltaTime.
     
  7. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Yep, I have such a system. I track my game objects and send them messages when I want them to pause/resume etc. Works great, and I can have physics and other stuff running while "paused", if I want to.