While the answers here work for the most part, these don’t really provide lerp functionality. The power of Linear Interpolation (lerp) is that you can feed in a ratio (0 to 1) of the completeness from start point to end point.
Baste’s answer will get you from the start point to the end point, but you can’t get back. It also doesn’t consider that the waypoints could not be in a straight line (his journeyLength is the end - start… well, what if they’re the same point?).
Here’s a more robust solution that can handle whatever waypoint positions you feed it AND you can feed in any ratio, making it an actual lerp function. Unfortunately, this comes at a cost, but unless your waypoint list has a lot of waypoints you should be OK. I’ll work out an optimized one when I have a chance. Check GitHub - Naphier/NGTools: Unity Editor Extensions and Tools by Napland Games or Vector3 Multilerp (multiple lerps) for handline waypoints. · GitHub for updates.
using System;
using UnityEngine;
namespace NG
{
public static class Vector3Ext
{
#region Multilerp
// This is not very optimized. There are at least n + 1 and at most 2n Vector3.Distance
// calls (where n is the number of waypoints).
public static Vector3 MultiLerp(Vector3[] waypoints, float ratio)
{
Vector3 position = Vector3.zero;
float totalDistance = waypoints.MultiDistance();
float distanceTravelled = totalDistance * ratio;
int indexLow = GetVectorIndexFromDistanceTravelled(waypoints, distanceTravelled);
int indexHigh = indexLow + 1;
// we're done
if (indexHigh > waypoints.Length - 1)
return waypoints[waypoints.Length - 1];
// calculate the distance along this waypoint to the next
Vector3[] completedWaypoints = new Vector3[indexLow + 1];
for (int i = 0; i < indexLow + 1; i++)
{
completedWaypoints _= waypoints*;*_
}
float distanceCoveredByPreviousWaypoints = completedWaypoints.MultiDistance();
float distanceTravelledThisSegment =
distanceTravelled - distanceCoveredByPreviousWaypoints;
float distanceThisSegment = Vector3.Distance(waypoints[indexLow], waypoints[indexHigh]);
float currentRatio = distanceTravelledThisSegment / distanceThisSegment;
position = Vector3.Lerp(waypoints[indexLow], waypoints[indexHigh], currentRatio);
return position;
}
public static float MultiDistance(this Vector3[] waypoints)
{
float distance = 0f;
for (int i = 0; i < waypoints.Length; i++)
{
if (i + 1 > waypoints.Length - 1)
break;
distance += Vector3.Distance(waypoints*, waypoints[i + 1]);*
}
return distance;
}
public static int GetVectorIndexFromDistanceTravelled(
Vector3[] waypoints, float distanceTravelled)
{
float distance = 0f;
for (int i = 0; i < waypoints.Length; i++)
{
if (i + 1 > waypoints.Length - 1)
return waypoints.Length - 1;
float segmentDistance = Vector3.Distance(waypoints*, waypoints[i + 1]);*
if (segmentDistance + distance > distanceTravelled)
{
return i;
}
distance += segmentDistance;
}
return waypoints.Length - 1;
}
#endregion
}
}