Advertisement
Guest User

MoveAlongSpline.cs

a guest
Feb 27th, 2020
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. #pragma warning disable 0649
  2.  
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5.  
  6. public class MoveAlongSpline : MonoBehaviour
  7. {
  8.     [SerializeField] private float _speed = 1.0f;
  9.     [SerializeField] private SpriteShapeController _spriteShapeController;
  10.  
  11.     private Vector3[] _positions;
  12.     private LTDescr _tween;
  13.  
  14.     private void Start()
  15.     {
  16.         Debug.Assert(transform.parent == _spriteShapeController.transform, $"Please make {name} a direct child of {_spriteShapeController}", gameObject);
  17.         Spline spline = _spriteShapeController.spline;
  18.         int bezierCount = spline.GetPointCount() - 1;
  19.         _positions = new Vector3[bezierCount * 4];
  20.         for (int i = 0; i < bezierCount; ++i)
  21.         {
  22.             // Get points in local space
  23.             Vector2 startPoint = _spriteShapeController.spline.GetPosition(i);
  24.             Vector2 startControl = startPoint + (Vector2)_spriteShapeController.spline.GetRightTangent(i);
  25.             Vector2 endPoint = _spriteShapeController.spline.GetPosition(i + 1);
  26.             Vector2 endControl = endPoint + (Vector2)_spriteShapeController.spline.GetLeftTangent(i + 1);
  27.  
  28.             // Note: the control for the end and start are reversed! This is just a quirk of the LeanTween API.
  29.             _positions[4 * i + 0] = startPoint;
  30.             _positions[4 * i + 1] = endControl;
  31.             _positions[4 * i + 2] = startControl;
  32.             _positions[4 * i + 3] = endPoint;
  33.         }
  34.  
  35.         // Start the tween, you can control it (e.g. pause, resume) using this handle
  36.         _tween = LeanTween.moveLocal(gameObject, _positions, 0.0f)
  37.             .setLoopPingPong()
  38.             .setEase(LeanTweenType.easeInOutQuad)
  39.             .setSpeed(_speed)
  40.             .setPassed(Time.time);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement