You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

70 lines
1.8 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BansheeGz.BGSpline.Components;
using BansheeGz.BGSpline.Curve;
public class Lighthouse : MonoBehaviour
{
public BGCurve splineCurve;
public List<float> splineDistantPoints = new List<float>();
public float speed = 200f;
public float timePadding = 0.5f;
public float timeBetweenPoints = 3f;
public AnimationCurve animationCurve;
private BGCcCursor splineCursor;
private int targetPoint = 0;
private bool reverseDirection = false;
void Start()
{
splineCursor = splineCurve.GetComponent<BGCcCursor>();
if (splineDistantPoints == null || splineDistantPoints.Count == 0)
{
splineDistantPoints.Add(0f);
for (int i=1; i<splineCurve.PointsCount; i++)
{
BGCurvePointI point = splineCurve.Points[i];
float distance = 0f;
Vector3 temp = splineCursor.Math.CalcPositionByClosestPoint(point.PositionWorld, out distance);
splineDistantPoints.Add(distance);
}
splineDistantPoints.Add(splineCursor.Math.GetDistance());
}
splineCursor.Distance = 0f;
moveToNextPoint();
}
public void moveToNextPoint()
{
LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
if (reverseDirection)
targetPoint--;
else
targetPoint++;
if (targetPoint >= splineDistantPoints.Count)
{
if (splineCurve.Closed)
{
targetPoint = 1;
splineCursor.Distance = 0f;
}
else
{
targetPoint = splineDistantPoints.Count - 2;
reverseDirection = !reverseDirection;
}
}
float start = splineCursor.Distance;
float end = splineDistantPoints[targetPoint];
float distance = Mathf.Abs(end - start);
LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{
splineCursor.Distance = val;
}).setEase(animationCurve).setOnComplete(moveToNextPoint);
});
}
}