Hi,
I'm currently testing Curvy to make a creature roam on a path with multiple spline connected.
Here is a screenshot of the scene with in red the path that my creature choose to follow another connected spline:
Here the detail of the behavior:
- it begins on the main spline (the white large closed spline).
- when it reachs the first connection with the blue spline, it works, it goes on it and keep following the path.
- BUG: at the end of the blue spline, it goes back on the main spline but it will teleport at the first control point and I don't understand why so I may miss something with the example I looked.
Here is the code exectued at each control point :
My goal with the code is to check if the path will be smooth enough to validate a new spline. Additionnaly if the current spline is closed or the current control point is not the last point, the spline itself is added to the pool to be randomized later as the next spline. But if it's not possible to continue on the current spline because it's the last point, I make sure that the controller will choose another spline even if it's not smooth.
I'm currently testing Curvy to make a creature roam on a path with multiple spline connected.
Here is a screenshot of the scene with in red the path that my creature choose to follow another connected spline:
Here the detail of the behavior:
- it begins on the main spline (the white large closed spline).
- when it reachs the first connection with the blue spline, it works, it goes on it and keep following the path.
- BUG: at the end of the blue spline, it goes back on the main spline but it will teleport at the first control point and I don't understand why so I may miss something with the example I looked.
Here is the code exectued at each control point :
Code:
public void OnControlPointReached_Path(CurvySplineMoveEventArgs e)
{
if (!e.ControlPoint.Connection)
return;
List<CurvySplineSegment> valideControlPoints = new List<CurvySplineSegment>();
List<CurvySplineSegment> others = e.ControlPoint.Connection.OtherControlPoints(e.ControlPoint);
for (int i = 0; i < others.Count; ++i)
{
if (e.AngleTo(others[i]) < 90) // Smooth enough
{
valideControlPoints.Add(others[i]);
}
}
if (e.Spline.IsClosed || !e.ControlPoint.IsLastControlPoint) // Can continue on the same spline
{
valideControlPoints.Add(e.ControlPoint); // Add current spline
}
else // Must change Spline
{
if (valideControlPoints.Count == 0) // Find solution even if the LD is not properly setup
{
CurvySplineSegment bestControlPoint = others[0];
for (int i = 1; i < others.Count; ++i)
{
if (e.AngleTo(others[i]) < e.AngleTo(bestControlPoint))
{
bestControlPoint = others[i];
}
}
valideControlPoints.Add(bestControlPoint);
}
}
// Find new spline
int randomIndex = Random.Range(0, valideControlPoints.Count);
CurvySplineSegment controlPoint = valideControlPoints[randomIndex];
if (controlPoint != e.ControlPoint)
{
e.Follow(controlPoint); // Follow the new spline
// Set the controller to use the new spline
Controller.Spline = e.Spline;
Controller.RelativePosition = e.TF;
}
}
My goal with the code is to check if the path will be smooth enough to validate a new spline. Additionnaly if the current spline is closed or the current control point is not the last point, the spline itself is added to the pool to be randomized later as the next spline. But if it's not possible to continue on the current spline because it's the last point, I make sure that the controller will choose another spline even if it's not smooth.