- Home /
Enemy nearest waypoint detector using LINQ, I need help updating the patrol array index using info from LINQ
Hello all!
Sorry if this seems trivial, but I've been coding all night. I just reached this issue on my list and my brain is running out of steam!
I currently have an enemy who patrols a top-down 2D space by going from waypoint to waypoint (using waypoint objects in an array, going from 0 then 1 then 2, etc)
if (Vector3.Distance(transform.position, currentWP.position) < .1f)
{
if (currentWPIndex + 1 < waypoints.Length)
{
currentWPIndex++;
}
else
{
currentWPIndex = 0;
}
currentWP = waypoints[currentWPIndex];
}
I also have a FindNearestWaypoint function that utilizes LINQ to locate the waypoint that is nearest to the enemy. This will get called when the enemy loses aggro, so it can follow its path from the nearest waypoint. Right now, when the enemy loses aggro, it attempts to go to the waypoint it was heading toward before aggro; meaning if it was at waypoint1 heading toward waypoint2 and I aggro it to chase me around all sorts of twists and turns across the map near waypoint20 and I lose aggro, it will attempt to walk in a straight line back toward waypoint2.
GameObject FindNearestWaypoint() {
GameObject wpOBJ;
Vector3 position = transform.position;
wpOBJ = GameObject.FindGameObjectsWithTag("Waypoint")
.OrderBy(o => (o.transform.position - position).sqrMagnitude)
.FirstOrDefault();
Debug.Log(wpOBJ);
//currentWP = wpOBJ.transform;
//currentWPIndex = ?;
return wpOBJ;
}
Basically, what I want to do is figure out the corresponding index number for the returned wpOBJ and update the patrol index. The lines of commented out code were my attempts to take a crack at it, but I gotta be honest, I just don't have it in me! Once adding the currentWP = wpOBJ.transform;
line, the enemy would go to the nearest waypoint after losing aggro as intended, but then it would just head to the previously stored index (using the example from the last section: starting at waypoint1, heading toward waypoint2, taking the enemy to waypoint20 then losing aggro ... the enemy would go to waypoint20, like intended, but rather than going to waypoint21 next, they would go back to waypoint2).
UPDATE: I tried manually making a converter which didn't work :/
void ObjToIndex() {
switch (wpOBJ.gameObject.name) {
case "WP_1":
currentWPIndex = 0;
break;
case "WP_2":
currentWPIndex = 1;
break;
case "WP_3":
currentWPIndex = 2;
break;
etc
Also just tried this, but it didn't change anything.
public int ObjToIndex() {
switch (wpOBJ.gameObject.name) {
case "WP_1":
currentWPIndex = 0;
return currentWPIndex;
case "WP_2":
currentWPIndex = 1;
return currentWPIndex;
case "WP_3":
currentWPIndex = 2;
return currentWPIndex;
~ Thank you to anyone who can help me logic through this a bit! ~
P.S. I know that this isn't a proper pathfinding method (I plan on using A*) and that my solutions aren't the most efficient; I'm just rapidly iterating on a prototype for a better sense of game rules and mechanics before I get started on my actual project. Point being: quick and dirty solutions are absolutely welcome! :)
Answer by Hiyek · Apr 12, 2018 at 11:24 PM
I tried adding currentWPIndex = System.Array.IndexOf(waypoints,wpOBJ);
to no avail.
Am I on the right track?