Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning disable 0649
- using UnityEngine;
- using UnityEngine.U2D;
- public class MoveAlongSpline : MonoBehaviour
- {
- [SerializeField] private float _speed = 1.0f;
- [SerializeField] private SpriteShapeController _spriteShapeController;
- private Vector3[] _positions;
- private LTDescr _tween;
- private void Start()
- {
- Debug.Assert(transform.parent == _spriteShapeController.transform, $"Please make {name} a direct child of {_spriteShapeController}", gameObject);
- Spline spline = _spriteShapeController.spline;
- int bezierCount = spline.GetPointCount() - 1;
- _positions = new Vector3[bezierCount * 4];
- for (int i = 0; i < bezierCount; ++i)
- {
- // Get points in local space
- Vector2 startPoint = _spriteShapeController.spline.GetPosition(i);
- Vector2 startControl = startPoint + (Vector2)_spriteShapeController.spline.GetRightTangent(i);
- Vector2 endPoint = _spriteShapeController.spline.GetPosition(i + 1);
- Vector2 endControl = endPoint + (Vector2)_spriteShapeController.spline.GetLeftTangent(i + 1);
- // Note: the control for the end and start are reversed! This is just a quirk of the LeanTween API.
- _positions[4 * i + 0] = startPoint;
- _positions[4 * i + 1] = endControl;
- _positions[4 * i + 2] = startControl;
- _positions[4 * i + 3] = endPoint;
- }
- // Start the tween, you can control it (e.g. pause, resume) using this handle
- _tween = LeanTween.moveLocal(gameObject, _positions, 0.0f)
- .setLoopPingPong()
- .setEase(LeanTweenType.easeInOutQuad)
- .setSpeed(_speed)
- .setPassed(Time.time);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement