Last active
August 29, 2015 14:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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