Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace JasonStorey
- {
- public class MoveBackAndForth : MonoBehaviour
- {
- [Tooltip("Which method to use to move")]
- [SerializeField]
- public MoveMethod Method;
- [Tooltip("The Axis to move along")]
- [SerializeField]
- public Vector3 Axis = new Vector3(1,0,0);
- [Tooltip("Amount of units to cover in XSeconds")]
- [SerializeField,Range(0,50)]
- public float Amount = 10;
- [Tooltip("Speed to move back and forth")]
- [SerializeField,Range(0,10)]
- public float Speed = 1;
- [Tooltip("When moving with a curve, the curve to use")]
- [SerializeField]
- AnimationCurve Curve = AnimationCurve.Linear(0,0,1,1);
- [Tooltip("When using smooth damp, what smoothing to apply")]
- [SerializeField,Range(0,3)]
- public float Smoothing = 0.3f;
- void Awake()
- {
- CreateMoveMethods();
- ResetCenter();
- Recalculate();
- }
- [ContextMenu("ResetCenter")]
- public void ResetCenter() => SetCenter(transform.position);
- void CreateMoveMethods() =>
- Methods =
- new Dictionary<MoveMethod, Func<Vector3, Vector3, float,Vector3>>
- {
- {MoveMethod.PingPong, BackAndForth.PingPong},
- {MoveMethod.SineWave, BackAndForth.SineMove},
- {MoveMethod.Curved, Curved},
- {MoveMethod.SmoothDamp,SmoothDamp}
- };
- void OnValidate() => Recalculate();
- void Update() => Move();
- void Move() => transform.position = CalculatePosition(A, B, Speed);
- #region Plumbing
- Vector3 CalculatePosition(Vector3 a, Vector3 b, float speed) => Methods[Method].Invoke(a, b, speed);
- Vector3 Curved(Vector3 a, Vector3 b, float speed) => BackAndForth.Curved(a, b, speed, Curve);
- Vector3 SmoothDamp(Vector3 a, Vector3 b, float speed) =>
- BackAndForth.SmoothDamp(a, b, transform.position, speed, ref Velocity, Smoothing);
- public void Recalculate() => SetCenter(Center);
- Vector3 Offset => Axis * Amount / 2;
- Vector3 A,B,Center,Velocity;
- void SetCenter(Vector3 center)
- {
- Center = center;
- A = center - Offset;
- B = center + Offset;
- }
- #if UNITY_EDITOR
- void OnDrawGizmosSelected()
- {
- if (!Application.isPlaying)
- {
- SetCenter(transform.position);
- Recalculate();
- }
- Gizmos.DrawSphere(Center,0.3f);
- Handles.DrawAAPolyLine(4,A,B);
- }
- #endif
- public enum MoveMethod { PingPong,SineWave,Curved,SmoothDamp }
- Dictionary<MoveMethod, Func<Vector3, Vector3, float, Vector3>> Methods;
- #endregion
- }
- public class BackAndForth
- {
- public static Vector3 PingPong(Vector3 a, Vector3 b, float speed) => MoveBetween(a, b, PingPongTime(speed));
- public static Vector3 SineMove(Vector3 a, Vector3 b, float speed) => MoveBetween(a, b, SinTime(speed));
- public static Vector3 Curved(Vector3 a, Vector3 b, float speed, AnimationCurve curve) => MoveBetween(a, b, curve.Evaluate(PingPongTime(speed)));
- public static Vector3 SmoothDamp(Vector3 a,Vector3 b,Vector3 currentPosition,float speed,ref Vector3 velocity,float smoothing = 0.3f)
- {
- Vector3 target = PingPong(a, b, speed);
- return Vector3.SmoothDamp(currentPosition,target,ref velocity,smoothing);
- }
- static Vector3 MoveBetween(Vector3 a, Vector3 b, float delta) => Vector3.Lerp(a, b, delta);
- static float PingPongTime(float speed) => Mathf.PingPong(Time.time * speed,1);
- static float SinTime(float speed) => (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement