Ren's Documentation - kreetin7/SuperHotClone2018 GitHub Wiki
Slowing down global time based on a variable in C#
In order to slow down time based on any variable, you want to first create a script called TimeManager and place it on an empty gameObject of the same name. In the script, declare a variable called TimeTarget and a variable called TimeSpeed. TimeTarget is the fraction of normal time at which you want time to actually move. So if time normally runs at an 1, then setting TimeTarget to 0.5 would make time run twice as slow as normal. TimeSpeed is the rate at which you want time to move between targets. The smaller the TimeSpeed, the faster it will switch. Set the time manager as an instance. You want this script to be called from everywhere and effect everything, so set it as an instance by placing "public static TimeManager instance" with your variables, and set "instance = this" in start. Now in Update we finally set Time.TimeScale. We use the function Mathf.MoveTowards, which literally moves one number towards another number. This has three parameters, the current float, the float we want to movetowards, and the speed at which we want to move towards that target. So quite literally you write "Time.timeScale = Mathf.MoveTowards(Time.timeScale, TimeTarget, TimeSpeed)" which sets TimeScale from whatever value it is currently at, towards the target we set, at the speed we set. So now if you want to slowdown time when you press X for example, under the Input.GetKey if statement, you write "TimeManager.instance.TimeTarget = .05f" and the Timemanager reads that and slows down global time. If you want time to speed up, you simply set the time target back to 1. If you want something to be unaffected by the changing time scale, you can use "Time.unscaledDeltaTime" which reads the amount of real time since you began the scene instead of the relative time that timeScale has set. There are a few special lines for certain things you want to be unaffected, such as WaitforSecondsRealTime in a coroutine. You can do any of these from any script.