0
\$\begingroup\$

I try to make a navigation/waypoint system. Well I have a concept but no idea how I could create this system. Well here's an image and an explanation: The concept art

You can see 3 waypoints as well as an goal and a player. The function should take the player position (X,Y,Z), a waypoint position array as well as an goal position. If the player is now on the position like in the image then it should output the second waypoint position because it's the closest waypoint in the direction of the goal. If the player is on the second waypoint position then it needs to output the third waypoint and so on to the finish. Well that's my crude concept. I hope that somebody can help! Thanks in advance :D


NOTE: You can add any values you want to add the most important thing is that it helps me :)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Are the waypoints always on a straight line? "Direction of the goal" is an extremely finnicky way to define something like this. \$\endgroup\$ – Tyyppi_77 Feb 28 '20 at 20:52
  • \$\begingroup\$ That's a good question! No, they aren't. \$\endgroup\$ – Leocat Feb 28 '20 at 21:27
0
\$\begingroup\$

The best way I can think of is having a list of waypoints in order from furthest to closest. Then given a waypoint you know you already passed you could just find the closest waypoint that you haven't passed yet. Something like.

public int nextWaypointIndex(Vector3 playerLocation, List<Vector3> orderedWaypoints,
                                int passedThisWaypoint) {

    int nextWaypoint = passedThisWaypoint + 1;
    float lowestDistance = Vector3.Distance(playerLocation, waypoints[nextWaypoint]);

    for (int i = passedThisWaypoint + 2; i < waypoints.Count; i++) {
        if (Vector3.Distance(playerLocation, waypoints[i]) <= lowestDistance) {
            nextWaypoint = i;
            lowestDistance = Vector3.Distance(playerLocation, waypoints[i]);
        }
    }

    return nextWaypoint;
}
\$\endgroup\$
1
  • \$\begingroup\$ I will take a look at that thanks. \$\endgroup\$ – Leocat Mar 2 '20 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.