- Home /
How to find total distance from start to end of an array which stores positions in vector3?
If an array contains positions of a path then how to find total distance of the path
Answer by Priyanka-Rajwanshi · Apr 19, 2020 at 03:05 PM
@shivanshsaini17 Use Vector3.Distance for finding distance between two points. Use can try this code snippet:
public Transform []path;
float pathDistance;
void CalculatePathDistance()
{
for (int i = 0; i < path.Length - 1; i++)
{
pathDistance += Vector3.Distance(path[i].position, path[i + 1].position);
}
}
Answer by jayghost1983 · Apr 19, 2020 at 12:47 PM
Off the top of my head, I would do something like...
float distanceTotal = 0;
int index = 1;
Transform startPosition = arrayPositions[0].transform;
foreach(position in arrayPositions)
{
distanceTotal = (arrayPosition[index].transform.position + position);
index += 1;
}
distanceTotal = distanceTotal - startPosiiton.position;
the exact coding, of course, would be more . . . thought out? but that should give you a starting point.
Your answer
Follow this Question
Related Questions
Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer
collider triggers transform position 1 Answer
Spawn objects at bottom of screen / camera 2 Answers
Make the character follow the images one by one smoothly 1 Answer
Help with this script? Keeps moving my objects around in run-time? 0 Answers