Advertisement
Zac_McDonald

Unity C# - IntervalClampUpdate

Mar 25th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. // NOTE: Also needs the IntervalClampHelper Script: https://pastebin.com/dPUKwRfs
  2. [RequireComponent(typeof(IntervalClampHelper))]
  3.     public class IntervalClampUpdate : MonoBehaviour
  4.     {
  5.         // Needs to be set to run after the default time: Edit/Project Settings/Script Execution Order
  6.  
  7.         #region Public Fields
  8.         //[HideInInspector]                 // Uncomment if you don't want to see the current position in the inspector
  9.         public Vector3 actualPosition;      // Used by the helper to carry our actual position across frames
  10.         public float interval = 0.0625f;    // [1/16]
  11.         #endregion
  12.  
  13.         #region Private Fields
  14.         #endregion
  15.  
  16.         #region Unity Methods
  17.         protected void Start ()
  18.         {
  19.             // Set the inital position
  20.             actualPosition = transform.position;
  21.         }
  22.  
  23.         protected void LateUpdate ()
  24.         {
  25.             // This runs at the end of a frame, before rendering.
  26.             // Here we store the current position so we can carry it to
  27.             // the following frame. We then clamp the position as per normal.
  28.             actualPosition = transform.position;
  29.             Vector3 lockedPosition = actualPosition;
  30.             lockedPosition.x = RoundToInterval(lockedPosition.x, interval);
  31.             lockedPosition.y = RoundToInterval(lockedPosition.y, interval);
  32.             lockedPosition.z = RoundToInterval(lockedPosition.z, interval);
  33.             transform.position = lockedPosition;
  34.         }
  35.         #endregion
  36.  
  37.         #region Public Methods
  38.         #endregion
  39.  
  40.         #region Private Methods
  41.         private float RoundToInterval (float value, float interval)             // Will round a number to the nearest interval
  42.         {
  43.             int numberOfIntervals = Mathf.RoundToInt(value / interval);
  44.             float roundedValue = numberOfIntervals * interval;
  45.             return roundedValue;
  46.         }
  47.  
  48.         private float RoundUpToInterval(float value, float interval)            // Will round a number up to the nearest interval
  49.         {
  50.             int numberOfIntervals = Mathf.RoundToInt((value + interval / 2) / interval);
  51.             float roundedValue = numberOfIntervals * interval;
  52.             return roundedValue;
  53.         }
  54.  
  55.         private float RoundDownToInterval(float value, float interval)          // Will round a number down to the nearest interval
  56.         {
  57.             int numberOfIntervals = Mathf.RoundToInt((value - interval/2) / interval);
  58.             float roundedValue = numberOfIntervals * interval;
  59.             return roundedValue;
  60.         }
  61.         #endregion
  62.  
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement