//helper array for curved paths, includes control points for waypoint array
Vector3[] points;
//taken and modified from
//http://code.google.com/p/hotween/source/browse/trunk/Holoville/HOTween/Core/Path.cs
//draws the full path
void DrawCurvedLine(int curvedSmoothIndex)
{
Gizmos.color = curvedWayLineColor;
Transform[] waypoints = GetWayPoints();
if (waypoints.Length < 2) return;
points = new Vector3[waypoints.Length + 2];
for (int i = 0; i < waypoints.Length; i++)
{
points[i + 1] = waypoints[i].position;
}
points[0] = points[1];
points[points.Length - 1] = points[points.Length - 2];
Gizmos.color = curvedWayLineColor;
Vector3[] drawPs;
Vector3 currPt;
// Store draw points.
int subdivisions = points.Length * curvedSmoothIndex;
drawPs = new Vector3[subdivisions + 1];
for (int i = 0; i <= subdivisions; ++i)
{
float pm = i / (float)subdivisions;
currPt = GetPoint(pm);
drawPs[i] = currPt;
}
// Draw path.
Vector3 prevPt = drawPs[0];
for (int i = 1; i < drawPs.Length; ++i)
{
currPt = drawPs[i];
Gizmos.DrawLine(currPt, prevPt);
prevPt = currPt;
}
}
//taken from
//http://code.google.com/p/hotween/source/browse/trunk/Holoville/HOTween/Core/Path.cs
// Catmull-Rom spline
// Gets the point on the curve at the given percentage (0 to 1).
// t: The percentage (0 to 1) at which to get the point.
private Vector3 GetPoint(float t)
{
int numSections = points.Length - 3;
int tSec = (int)System.Math.Floor(t * numSections);
int currPt = numSections - 1;
if (currPt > tSec)
{
currPt = tSec;
}
float u = t * numSections - currPt;
Vector3 a = points[currPt];
Vector3 b = points[currPt + 1];
Vector3 c = points[currPt + 2];
Vector3 d = points[currPt + 3];
return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
}