Advertisement
orikalin

Untitled

Feb 3rd, 2022
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class TimeController : MonoBehaviour
  7. {
  8.     public float slowdownFactor = 0.05f;
  9.     public float slowdownLength = 2f;
  10.     public AnimationCurve enterSlowMo;
  11.     public AnimationCurve exitSlowMo;
  12.     [HideInInspector]public bool slow = false;
  13.     public IEnumerator theActiveCoRoutine;
  14.  
  15.     public void DoSlowMotion()
  16.     {
  17.         if(theActiveCoRoutine == null && !slow)
  18.         {
  19.             theActiveCoRoutine = EnterSlowMo();
  20.             StartCoroutine(theActiveCoRoutine);          
  21.         }
  22.         else if (slow)
  23.         {
  24.             StopCoroutine(theActiveCoRoutine);
  25.             theActiveCoRoutine = (ExitSlowMo());
  26.             StartCoroutine(theActiveCoRoutine);
  27.         }
  28.     }
  29.  
  30.     public IEnumerator EnterSlowMo()
  31.     {
  32.        
  33.         float t = 0f;
  34.         float currentTimeScale = Time.timeScale;
  35.         Debug.Log("Entering slow mo");
  36.         while (t < 0.5)
  37.         {
  38.             Time.timeScale = Mathf.Lerp(currentTimeScale, 0.25f, enterSlowMo.Evaluate(t));
  39.             t += Time.unscaledDeltaTime;
  40.             yield return null;
  41.         }
  42.         Time.timeScale = 0.25f;
  43.         slow = true;
  44.  
  45.     }
  46.     public IEnumerator ExitSlowMo()
  47.     {
  48.         slow = false;
  49.         float t = 0f;
  50.         float currentTimeScale = Time.timeScale;
  51.         Debug.Log("Exiting slow mo");
  52.         while (t < 0.5)
  53.         {
  54.             Time.timeScale = Mathf.Lerp(currentTimeScale, 1, exitSlowMo.Evaluate(t));
  55.             t += Time.unscaledDeltaTime;
  56.             yield return null;
  57.         }
  58.         Time.timeScale = 1;
  59.         theActiveCoRoutine = null;
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement