Skip to content

Instantly share code, notes, and snippets.

@aberloni
Last active August 29, 2015 14:19
Show Gist options
  • Save aberloni/7fb9c4f18470f33fb097 to your computer and use it in GitHub Desktop.
Save aberloni/7fb9c4f18470f33fb097 to your computer and use it in GitHub Desktop.
Script to have a deltatime and timescale independent to Time.deltaTime in Unity
using UnityEngine;
using System.Collections;
public class GameTime : MonoBehaviour {
public static GameTime manager;
public float timeScale = 1;
float elapsedTime;
public static float deltaTime;
void Awake(){
manager = this;
}
// Update is called once per frame
void Update () {
deltaTime = Time.deltaTime * timeScale;
if(Input.GetKeyDown(KeyCode.KeypadPlus)){
timeScale += 0.5f;
}
if(Input.GetKeyDown(KeyCode.KeypadMinus)){
timeScale -= 0.5f;
}
if(Input.GetKeyUp(KeyCode.Y)){
timeScale = (timeScale == 1f) ? 10f : 1f;
}
elapsedTime += Time.deltaTime * timeScale;
elapsedTime = Mathf.Max(0, elapsedTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment