Advertisement
apieceoffruit

MoveBackAndForth

May 10th, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace JasonStorey
  8. {
  9.     public class MoveBackAndForth : MonoBehaviour
  10.     {
  11.         [Tooltip("Which method to use to move")]
  12.         [SerializeField]
  13.         public MoveMethod Method;
  14.        
  15.         [Tooltip("The Axis to move along")]
  16.         [SerializeField]
  17.         public Vector3 Axis = new Vector3(1,0,0);
  18.  
  19.         [Tooltip("Amount of units to cover in XSeconds")]
  20.         [SerializeField,Range(0,50)]
  21.         public float Amount = 10;
  22.  
  23.         [Tooltip("Speed to move back and forth")]
  24.         [SerializeField,Range(0,10)]
  25.         public float Speed = 1;
  26.  
  27.         [Tooltip("When moving with a curve, the curve to use")]
  28.         [SerializeField]
  29.         AnimationCurve Curve = AnimationCurve.Linear(0,0,1,1);
  30.  
  31.         [Tooltip("When using smooth damp, what smoothing to apply")]
  32.         [SerializeField,Range(0,3)]
  33.         public float Smoothing = 0.3f;
  34.         void Awake()
  35.         {
  36.             CreateMoveMethods();
  37.             ResetCenter();
  38.             Recalculate();
  39.         }
  40.  
  41.         [ContextMenu("ResetCenter")]
  42.         public void ResetCenter() => SetCenter(transform.position);
  43.        
  44.         void CreateMoveMethods() =>
  45.             Methods =
  46.                 new Dictionary<MoveMethod, Func<Vector3, Vector3, float,Vector3>>
  47.                 {
  48.                     {MoveMethod.PingPong, BackAndForth.PingPong},
  49.                     {MoveMethod.SineWave, BackAndForth.SineMove},
  50.                     {MoveMethod.Curved, Curved},
  51.                     {MoveMethod.SmoothDamp,SmoothDamp}
  52.                 };
  53.  
  54.         void OnValidate() => Recalculate();
  55.  
  56.         void Update() => Move();
  57.  
  58.         void Move() => transform.position = CalculatePosition(A, B, Speed);
  59.  
  60.         #region Plumbing
  61.        
  62.         Vector3 CalculatePosition(Vector3 a, Vector3 b, float speed) => Methods[Method].Invoke(a, b, speed);
  63.        
  64.         Vector3 Curved(Vector3 a, Vector3 b, float speed) => BackAndForth.Curved(a, b, speed, Curve);
  65.  
  66.         Vector3 SmoothDamp(Vector3 a, Vector3 b, float speed) =>
  67.             BackAndForth.SmoothDamp(a, b, transform.position, speed, ref Velocity, Smoothing);
  68.        
  69.         public void Recalculate() => SetCenter(Center);
  70.        
  71.         Vector3 Offset => Axis * Amount / 2;
  72.  
  73.  
  74.         Vector3 A,B,Center,Velocity;
  75.         void SetCenter(Vector3 center)
  76.         {
  77.             Center = center;
  78.             A = center - Offset;
  79.             B = center + Offset;
  80.         }
  81.        
  82. #if UNITY_EDITOR
  83.         void OnDrawGizmosSelected()
  84.         {
  85.             if (!Application.isPlaying)
  86.             {
  87.                 SetCenter(transform.position);
  88.                 Recalculate();
  89.             }
  90.             Gizmos.DrawSphere(Center,0.3f);
  91.             Handles.DrawAAPolyLine(4,A,B);
  92.         }
  93. #endif
  94.  
  95.         public enum MoveMethod { PingPong,SineWave,Curved,SmoothDamp }
  96.         Dictionary<MoveMethod, Func<Vector3, Vector3, float, Vector3>> Methods;
  97.        
  98.         #endregion
  99.     }
  100.  
  101.     public class BackAndForth
  102.     {
  103.         public static Vector3 PingPong(Vector3 a, Vector3 b, float speed) => MoveBetween(a, b, PingPongTime(speed));
  104.         public static Vector3 SineMove(Vector3 a, Vector3 b, float speed) => MoveBetween(a, b, SinTime(speed));
  105.         public static Vector3 Curved(Vector3 a, Vector3 b, float speed, AnimationCurve curve) => MoveBetween(a, b, curve.Evaluate(PingPongTime(speed)));
  106.         public static Vector3 SmoothDamp(Vector3 a,Vector3 b,Vector3 currentPosition,float speed,ref Vector3 velocity,float smoothing = 0.3f)
  107.         {
  108.             Vector3 target = PingPong(a, b, speed);
  109.             return Vector3.SmoothDamp(currentPosition,target,ref velocity,smoothing);
  110.         }
  111.        
  112.         static Vector3 MoveBetween(Vector3 a, Vector3 b, float delta) => Vector3.Lerp(a, b, delta);
  113.        
  114.         static float PingPongTime(float speed) => Mathf.PingPong(Time.time * speed,1);
  115.         static float SinTime(float speed) => (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement