I’m having a hard time pausing my game AND having time-dependent transitions for my PauseMenu’s buttons (like increase its size a bit gradually when mouse is over, and decreasing it back to normal when mouse leaves the button), since for pausing I use Time.timeScale=0.
Is there any other way to pause the game or its entities without affecting Time functions?
I thought about having a Pausable behavior which would be attached to any GameObject I want to be able to pause, and be something like:
var paused = false;
function Update()
{
if (!paused)
gameObject.BroadcastMessage("DoUpdate");
}
function FixedUpdate()
{
if (!paused)
gameObject.BroadcastMessage("DoFixedUpdate");
}
Then using DoUpdate/DoFixedUpdate instead of the standard functions for every GO with the Pausable behavior, and also using some other pause-aware Timer object instead of Time.time when needed (I guess Time.deltaTime would be OK to use).
I would introduce “realTimePassed” by doing Time.deltaTime / Time.timeScale and use that for your pause button code. You need to watch out for when timescale goes to 0, but apart from that, it should be fairly easy…
Well, that’s the deal: I do set Time.timeScale to zero in order to pause the simulation, but I want time-based transitions for my GUI in the pause menu (which is a couple of buttons: Resume and Quit). Yeah, I could live without them, but I want them to have the same look feel of my main menu buttons.
In the Big Bang Brain Games I used the DateTime class for this so I didn’t have to worry about floating point accuracy as realTimeSinceStartup got high. I am suspicious there is some Mono bug with this method on some machines based on some reports I’ve had, but it seems pretty darn rare.
using UnityEngine;
using System;
public class Timer
{
DateTime startTime;
public Timer()
{
Reset();
}
public void Reset()
{
startTime = DateTime.Now;
}
public float ElapsedSeconds()
{
TimeSpan span = DateTime.Now.Subtract(startTime);
return ((float)span.Ticks / (float)TimeSpan.TicksPerSecond);
}
public long ElapsedTicks()
{
TimeSpan span = DateTime.Now.Subtract(startTime);
return span.Ticks;
}
}
Usage:
Timer timer = new Timer();
Debug.Log(timer.ElapsedSeconds());
Should probably make those functions properties for cleanliness.