Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.Rendering.PostProcessing;
- [RequireComponent(typeof(PostProcessVolume))]
- public class Slowmotiom : MonoBehaviour
- {
- [SerializeField] private float _transitionTime;
- [Range(0, 1)]
- [SerializeField] private float _vignetteMaxIntensity;
- [Range(0, 1)]
- [SerializeField] private float _chromaticAberrationMaxIntensity;
- private PostProcessVolume _volume;
- private Vignette _vignette;
- private ChromaticAberration _chromaticAberration;
- void Start()
- {
- _volume = GetComponent<PostProcessVolume>();
- _volume.profile.TryGetSettings<Vignette>(out _vignette);
- _volume.profile.TryGetSettings<ChromaticAberration>(out _chromaticAberration);
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Q))
- {
- StartSlowdown();
- }
- if (Input.GetKeyDown(KeyCode.E))
- {
- StartAcceleration();
- }
- }
- public void StartSlowdown()
- {
- StartCoroutine(LerpValue(1, 0, _transitionTime, ChangeTimeScale));
- StartCoroutine(LerpValue(0, 1, _transitionTime, ChangeEffectIntensity));
- }
- public void StartAcceleration()
- {
- StartCoroutine(LerpValue(0, 1, _transitionTime, ChangeTimeScale));
- StartCoroutine(LerpValue(1, 0, _transitionTime, ChangeEffectIntensity));
- }
- private IEnumerator LerpValue(float startValue, float endValue, float duration, UnityAction<float> onLerping)
- {
- float elapsed = 0.0f;
- float nextValue;
- while (elapsed < duration)
- {
- nextValue = Mathf.Lerp(startValue, endValue, elapsed / duration);
- onLerping?.Invoke(nextValue);
- elapsed += Time.unscaledDeltaTime;
- yield return null;
- }
- onLerping?.Invoke(endValue);
- }
- private void ChangeTimeScale(float value)
- {
- Time.timeScale = value;
- }
- private void ChangeEffectIntensity(float value)
- {
- _vignette.intensity.value = Mathf.Clamp(value, 0, _vignetteMaxIntensity);
- _chromaticAberration.intensity.value = Mathf.Clamp(value, 0, _chromaticAberrationMaxIntensity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement