- Home /
2nd independent deltaTime and timeScale variables, or a way to mimic this?
I need another deltaTime system, since I have objects that I need to slow up / slow down independently using time scaling. These different objects are active at the same time. I want to have in settings a slider for people to adjust these time scales independently. Specifically, I am talking about a slider for regular game speed, as well as a slider for reading speed. Dialogue/text needs to appear for a certain amount of scaled time, while other objects need to move at an independent time scale. For this, the simplest solution would be to have something like deltaTime2 and timeScale2. Is there a way to get a separate time scaling system, or the best way to mimic a new time scale system?
If I make my own deltaTime system that manages for multiple timeScales, I suppose I would also need to write my own WaitForSeconds function as well, that waits an amount of time appropriate to the differing possible timeScales, since I use WaitForSeconds a lot...
Am I correct in my assumption that timeScale simply is a constant multiplier of the deltaTime? Also, could I create my own WaitForSeconds that waits for this scaled amount of time to be passed (or past passed), is that how the original implementation works? Thanks!
Answer by Eno-Khaon · Jan 04 at 09:58 AM
If you're looking for more than just the difference between Time.time and Time.realtimeSinceStartup (and, by extension, WaitForSeconds() and WaitForSecondsRealtime()), then you would probably need to implement your own timekeeping groups. This is especially true depending on how you intend for Time.timeScale to influence (or not) FixedUpdate() cycles.
As an (incomplete) example of what it might look like:
public class TimeScalar
{
float scale;
public float deltaTime
{
get
{
return Time.unscaledDeltaTime * scale;
}
}
// ...
// Add other similar elements to read as needed, relative to unscaled values
// ...
public TimeScalar(float timeElapsedPerRealtimeSecond)
{
if(timeElapsedPerRealtimeSecond <= 0f)
{
Debug.LogWarning("TimeScalar(float): Value must be greater than 0");
timeElapsedPerRealtimeSecond = 1f;
}
scale = 1.0f / timeElapsedPerRealtimeSecond;
}
}
// Usage example:
// 5 seconds elapsed per 1 second realtime
TimeScalar dialogueScalar = new TimeScalar(5f);
// 1 second elapsed per 2 seconds realtime
TimeScalar objectScalar = new TimeScalar(0.5f);
// etc.
// ...
yield return new WaitForSecondsRealtime(duration * dialogueScalar.scale);