Splines are evaluated using the Evaluate method. It takes an argument of type double and can either return a SplineSample result or write to an existing result to preserve memory. The argument value must range between 0 and 1 (start and end of the spline).
Example:
SplineComputer spline = GetComponent<SplineComputer>();
SplineSample sample = spline.Evaluate(0.2); //This will allocate a new SplineSample object
Debug.Log(sample.position);
spline.Evaluate(0.3, ref sample); //This will reuse the sample object
Debug.Log(sample.position);
To find the percent of a spline that corresponds to a certain distance in world units along it, the Travel method can be used.
SplineComputer spline = GetComponent();
double distancePercentFromStart = spline.Travel(0.0, 15f, Spline.Direction.Forward);
double distancePercentFromEnd = spline.Travel(1.0, 15f, Spline.Direction.Backward);
SplineSample startSample= spline.Evaluate(distancePercentFromStart );
SplineSample endSample= spline.Evaluate(distancePercentFromEnd );
To calculate the length of a spline, use the CalculateLength method. It has two optional parameters – from and to [0-1]. If defined, the length of the spline will be calculated between the corresponding percentages.
Example:
SplineComputer spline = GetComponent();
float splineLength = spline.CalculateLength();
float halfPercentLength = spline.Calculate(0, 0.5);
When creating splines in runtime, it is important to use the SplinePoint constructor. The SplineComputer uses the SetPoint or SetPoints methods to write a single spline point or a list of points. If the provided points are not properly initialized, the resulting spline objects might have issues.
For example, the following code will create a spline that has the points in place but all points will have zero normals and zero sizes:
SplinePoint[] points = new SplinePoint[5];
for(int i = 0; i < points.Length; i++){
points[i].position = Vector3.forward * i;
}
spline.SetPoints(points);
What we need to do is to use the SplinePoint constructor instead:
SplinePoint[] points = new SplinePoint[5];
for(int i = 0; i < points.Length; i++){
points[i] = new SplinePoint(Vector3.forward * i);
}
spline.SetPoints(points);
If point normals are not defined correctly, the resulting geometry or generated objects might not look or behave properly. Spline normals are vectors which should be set to point roughly perpendicular to the spline direction.
Given the example above, normals can be set like this:
for(int i = 0; i < points.Length; i++){
points[i] = new SplinePoint(Vector3.forward * i);
points[i].normal = Vector3.up;
}
Finding the closest point on a spline to another point in world space is called projection. To project a point on a spline, use the Project method in the SplineComputer or the SplineUser component.
It returns a SplineSample object which contains the spline position, rotation and percent.
Example of SplineComputer.Project:
SplineComputer spline = GetComponent<SplineComputer>();
Vector3 worldPoint = new Vector3(3,4,5);
SplineSample sample = new SplineSample();
spline.Project(worldPoint, ref sample);