Created
February 26, 2024 10:32
-
-
Save Amitkapadi/f55dc4070dc0cb8e95067d9e48de831c to your computer and use it in GitHub Desktop.
[MultiSplineFollower] #unity #spline #DreamteckSplines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using Dreamteck.Splines; | |
using Dreamteck.Splines.Primitives; | |
using UnityEngine; | |
public class MultiSplineFollower : MonoBehaviour | |
{ | |
[System.Serializable] | |
public class SplineData | |
{ | |
public int index; | |
public SplineComputer splineComputer; | |
public Spline.Direction direction; | |
public Vector2 range; | |
} | |
public SplineData[] splineDatas; | |
public SplineFollower splineFollower; | |
int currentSplineIndex = 0; | |
SplineData nextSpline, currentSpline; | |
float previousLerp = -1; | |
public float time; | |
void Update() | |
{ | |
ManualUpdate(time += (Time.deltaTime * 0.1f)); | |
} | |
void Start() | |
{ | |
currentSplineIndex = 0; | |
currentSpline = splineDatas[currentSplineIndex]; | |
if (splineDatas.Length > 1) | |
nextSpline = splineDatas[currentSplineIndex + 1]; | |
} | |
public void ManualUpdate(float lerp) | |
{ | |
if (lerp > previousLerp) | |
{ | |
if (currentSplineIndex < splineDatas.Length - 1) | |
{ | |
if (lerp > nextSpline.range.x) | |
{ | |
currentSplineIndex += 1; | |
currentSpline = splineDatas[currentSplineIndex]; | |
if (currentSplineIndex < splineDatas.Length - 1) | |
nextSpline = splineDatas[currentSplineIndex + 1]; | |
splineFollower.spline = currentSpline.splineComputer; | |
splineFollower.RebuildImmediate(); | |
} | |
splineFollower.SetPercent(Mathf.InverseLerp(currentSpline.range.x, currentSpline.range.y, lerp)); | |
} | |
else | |
{ | |
//Last Spline | |
splineFollower.SetPercent(Mathf.InverseLerp(currentSpline.range.x, currentSpline.range.y, lerp)); | |
} | |
} | |
else | |
{ | |
if (currentSplineIndex > 0) | |
{ | |
if (lerp < nextSpline.range.y) | |
{ | |
currentSplineIndex -= 1; | |
currentSpline = splineDatas[currentSplineIndex]; | |
if (currentSplineIndex > 0) | |
nextSpline = splineDatas[currentSplineIndex - 1]; | |
splineFollower.spline = currentSpline.splineComputer; | |
splineFollower.RebuildImmediate(); | |
} | |
splineFollower.SetPercent(Mathf.InverseLerp(currentSpline.range.x, currentSpline.range.y, lerp)); | |
} | |
else | |
{ | |
//Last Spline | |
splineFollower.SetPercent(Mathf.InverseLerp(currentSpline.range.x, currentSpline.range.y, lerp)); | |
} | |
} | |
previousLerp = lerp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment