GeeItSomeLaldy

Unity RisingFalling Curve relay

Jun 11th, 2019 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. public class AnimationCurveRelay : MonoBehaviour
  7. {
  8.     public AnimationCurve SignalCurve;
  9.     public float Signal = 0;
  10.     public float HighSignal = 1;
  11.     public float LowSignal = 0;
  12.     public UnityEvent OnHighSignal, OnLowSignal;
  13.     public bool IsHigh;
  14.     public void SetSignal(float v)
  15.     {
  16.         Signal = SignalCurve.Evaluate(v);
  17.         UpdateSignal();
  18.     }
  19.     private void UpdateSignal(bool force = false)
  20.     {
  21.         if ((force || !IsHigh) && Signal >= HighSignal) {
  22.             IsHigh = true;
  23.             OnHighSignal.Invoke();
  24.         }
  25.         if ((force || IsHigh) && Signal <= LowSignal)
  26.         {
  27.             IsHigh = false;
  28.             OnLowSignal.Invoke();
  29.         }
  30.     }
  31.     private void Start()
  32.     {
  33.         UpdateSignal(true);
  34.     }
  35. }
  36.  
  37. ---
  38.  
  39. using System.Collections;
  40. using System.Collections.Generic;
  41. using UnityEngine;
  42.  
  43. public class GlobalCycleManager : MonoBehaviour
  44. {
  45.     public AnimationCurve curve;
  46.     public float Scale = 1;
  47.     public float V = 0;
  48.     public FloatEvent OnValue;
  49.  
  50.     // Update is called once per frame
  51.     void Update()
  52.     {
  53.         V = curve.Evaluate(Time.timeSinceLevelLoad * Scale);
  54.         OnValue.Invoke(V);
  55.     }
  56. }
  57.  
  58.  
  59. [System.Serializable]
  60. public class FloatEvent : UnityEvent<float> { }
Add Comment
Please, Sign In to add comment