Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class TimeController : MonoBehaviour
- {
- public float slowdownFactor = 0.05f;
- public float slowdownLength = 2f;
- public AnimationCurve enterSlowMo;
- public AnimationCurve exitSlowMo;
- [HideInInspector]public bool slow = false;
- public IEnumerator theActiveCoRoutine;
- public void DoSlowMotion()
- {
- if(theActiveCoRoutine == null && !slow)
- {
- theActiveCoRoutine = EnterSlowMo();
- StartCoroutine(theActiveCoRoutine);
- }
- else if (slow)
- {
- StopCoroutine(theActiveCoRoutine);
- theActiveCoRoutine = (ExitSlowMo());
- StartCoroutine(theActiveCoRoutine);
- }
- }
- public IEnumerator EnterSlowMo()
- {
- float t = 0f;
- float currentTimeScale = Time.timeScale;
- Debug.Log("Entering slow mo");
- while (t < 0.5)
- {
- Time.timeScale = Mathf.Lerp(currentTimeScale, 0.25f, enterSlowMo.Evaluate(t));
- t += Time.unscaledDeltaTime;
- yield return null;
- }
- Time.timeScale = 0.25f;
- slow = true;
- }
- public IEnumerator ExitSlowMo()
- {
- slow = false;
- float t = 0f;
- float currentTimeScale = Time.timeScale;
- Debug.Log("Exiting slow mo");
- while (t < 0.5)
- {
- Time.timeScale = Mathf.Lerp(currentTimeScale, 1, exitSlowMo.Evaluate(t));
- t += Time.unscaledDeltaTime;
- yield return null;
- }
- Time.timeScale = 1;
- theActiveCoRoutine = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement