Created
November 19, 2022 12:29
-
-
Save xavierarpa/9119cc96dffba1387557ccdca854309d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| using XavHelpTo.Set; | |
| [Serializable] | |
| public struct SmoothTransition | |
| { | |
| #region Variables | |
| public const float MIN_RANGE = .1f; | |
| public const float MAX_RANGE = 10f; | |
| [field: SerializeField] | |
| public Vector2 Limit { get; private set; } | |
| [field: SerializeField, Range(MIN_RANGE, MAX_RANGE)] | |
| public float Intensity_delta { get; private set; } | |
| [field: SerializeField, Range(MIN_RANGE, MAX_RANGE)] | |
| public float Intensity_speed { get; private set; } | |
| [Header("Debug")] | |
| //[SerializeField] | |
| [HideInInspector] | |
| public float delta; | |
| //[SerializeField] | |
| [HideInInspector] | |
| public float deltaSpeed; | |
| //[SerializeField] | |
| [HideInInspector] | |
| public float current; | |
| //[SerializeField] | |
| [HideInInspector] | |
| public float target; | |
| //[field:SerializeField] | |
| [HideInInspector] | |
| public float Output { get; private set; } | |
| //[Header("Progress")] | |
| //[Space] | |
| //[SerializeField, Range(0f, 1f)] | |
| private float progress_target; | |
| //[SerializeField, Range(0f, 1f)] | |
| private float progress_ouput; | |
| #endregion | |
| #region Methods | |
| public void Update_Limit(Vector2 limit) => Limit = limit; | |
| public void Update_Intensity_Delta(float intensity) => Intensity_delta = intensity; | |
| public void Update_Intensity_Speed(float intensity) => Intensity_speed = intensity; | |
| /// <summary> | |
| /// Actualizamos los datos del movimiento, representa la actualización de la aceleración | |
| /// </summary> | |
| public void Update_Delta(float _delta) | |
| { | |
| delta = _delta; | |
| target += delta * Intensity_delta; | |
| target = target.MinMax(Limit); | |
| progress_target = ((Limit.x - target) / (Limit.x - Limit.y)); | |
| } | |
| /// <summary> | |
| /// Hacemos la actualización de la transición, modificando los datos hasta llegar a la salida | |
| /// </summary> | |
| public void Update_Output(float _current) | |
| { | |
| current = _current.MinMax(Limit); | |
| target = target.MinMax(Limit); | |
| deltaSpeed = (target - current).Positive(); | |
| //Hacer la transición | |
| Output = Mathf.MoveTowards( | |
| current, | |
| target, | |
| Time.deltaTime * Intensity_speed * deltaSpeed | |
| ); | |
| progress_ouput = ((Limit.x - Output) / (Limit.x - Limit.y)); | |
| } | |
| #endregion | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment