Advertisement
Lexvolk

Untitled

Jan 18th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.Rendering.PostProcessing;
  6.  
  7. [RequireComponent(typeof(PostProcessVolume))]
  8. public class Slowmotiom : MonoBehaviour
  9. {
  10.     [SerializeField] private float _transitionTime;
  11.     [Range(0, 1)]
  12.     [SerializeField] private float _vignetteMaxIntensity;
  13.     [Range(0, 1)]
  14.     [SerializeField] private float _chromaticAberrationMaxIntensity;
  15.  
  16.     private PostProcessVolume _volume;
  17.     private Vignette _vignette;
  18.     private ChromaticAberration _chromaticAberration;
  19.  
  20.     void Start()
  21.     {
  22.         _volume = GetComponent<PostProcessVolume>();
  23.         _volume.profile.TryGetSettings<Vignette>(out _vignette);
  24.         _volume.profile.TryGetSettings<ChromaticAberration>(out _chromaticAberration);
  25.     }
  26.  
  27.     private void Update()
  28.     {
  29.         if (Input.GetKeyDown(KeyCode.Q))
  30.         {
  31.             StartSlowdown();
  32.         }
  33.  
  34.         if (Input.GetKeyDown(KeyCode.E))
  35.         {
  36.             StartAcceleration();
  37.         }
  38.     }
  39.  
  40.     public void StartSlowdown()
  41.     {
  42.         StartCoroutine(LerpValue(1, 0, _transitionTime, ChangeTimeScale));
  43.         StartCoroutine(LerpValue(0, 1, _transitionTime, ChangeEffectIntensity));
  44.     }
  45.  
  46.     public void StartAcceleration()
  47.     {
  48.         StartCoroutine(LerpValue(0, 1, _transitionTime, ChangeTimeScale));
  49.         StartCoroutine(LerpValue(1, 0, _transitionTime, ChangeEffectIntensity));
  50.     }
  51.  
  52.     private IEnumerator LerpValue(float startValue, float endValue, float duration, UnityAction<float> onLerping)
  53.     {
  54.         float elapsed = 0.0f;
  55.         float nextValue;
  56.         while (elapsed < duration)
  57.         {
  58.             nextValue = Mathf.Lerp(startValue, endValue, elapsed / duration);
  59.             onLerping?.Invoke(nextValue);
  60.             elapsed += Time.unscaledDeltaTime;
  61.             yield return null;
  62.         }
  63.         onLerping?.Invoke(endValue);
  64.     }
  65.  
  66.     private void ChangeTimeScale(float value)
  67.     {
  68.         Time.timeScale = value;
  69.     }
  70.  
  71.     private void ChangeEffectIntensity(float value)
  72.     {
  73.         _vignette.intensity.value = Mathf.Clamp(value, 0, _vignetteMaxIntensity);
  74.         _chromaticAberration.intensity.value = Mathf.Clamp(value, 0, _chromaticAberrationMaxIntensity);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement