- Home /
How to get a "Traveled" vector point along a path with some width?
For example I have a path, that consists of 10 cross sections. Each cross section defines a center point and edge points. So in total I have 30 vector points that define my path, which the traveler X can travel. My goal is to receive the traveled distance along the center points, centerpoints[currentPoint].
My current approach relies on dot product, but as soon as the traveler goes away from the center I get incorrect results. I am currently not using the edge points, but they are there if needed, because they define the width limits to the path.
Here is the code of my approach:
int points = 10; // size of the path list
int pointNumber = 0; // find the closest point in path list to traveler
// Measure the total length of the path and store distance per segment
float accumulateDistance = 0;
for (int i = 0; i < path.Count; i++)
{
Vector3 p1 = path[(i) % points].centerPoint;
Vector3 p2 = path[(i + 1) % points].centerPoint;
if (p1 != null && p2 != null)
{
accumulateDistance += (p1 - p2).magnitude;
path[i].dist = accumulateDistance;
}
}
// Measure the traveled distance along path
Vector3 currentPoint = path[pointNumber].centerPoint;
Vector3 nextPoint;
if (r.currentWaypoint < path.Count - 1)
nextPoint = path[pointNumber + 1].currentPoint;
else
nextPoint = path[0].currentPoint;
Vector3 forward = (nextPoint - currentPoint);
Vector3 relativePos = traveler.transform.position - currentPoint;
float distAdd = Vector3.Dot(relativePos, forward.normalized);
progressDistance = path[pointNumber].dist + distAdd;
Comment
Answer by ifisch · Sep 02, 2020 at 11:33 PM
If you want the question answered, you should include a diagram.