Attempt to search a list of spots for the nearest one doesn't update every FixedUpdate

I’m currently writing a rudimentary AI for an enemy that will follow the player around a platform level and attempt to reach them, without using A* or anything like that.

I’m setting it up so the ‘AI’ works on three systems: if the player is on the exact same platform as the enemy, the enemy can just safely move in a straight line right to the player.

If the player is on a platform above the enemy, the enemy will try to head to the nearest JumpSpot (empties placed around the level for where the enemy will jump up/down a platform) and reach the player. Vice versa for if the player is below the enemy.

I’m currently writing the system for if the player is above the enemy, and it appears to detect where the nearest JumpSpot is perfectly – but the nearest one doesn’t change if I move the enemy around, although it does log the first nearest one every single frame. Why is this?

private void FixedUpdate()
    {
        if(Vector2.Distance(transform.position, player.transform.position) < 1.2f)  // Makes the enemy stop moving when it's close to the player so it doesn't push the player away.
        {
            tooClose = true;
        }
        else
        {
            tooClose = false;
        }

        if(!tooClose)
        {
            if (playerMove.currentPlatform == e_CurrentPlatform)    // IF PLAYER IS ON THE SAME PLATFORM AS THE ENEMY
            {
                moveVector = (player.transform.position - transform.position).normalized;   // convert the vector at which the enemy should move to either -1 or 1
                moveDir = moveVector.x;
                controller.Move(moveDir, false);    // use the 2D Character Controller to move the enemy to the player
            }
            else if (playerMove.currentPlatform > e_CurrentPlatform)    // IF PLAYER IS ON A PLATFORM ABOVE THE ENEMY
            {
                minJumpDist = 99;
                for(int i = 0; i < jumpSpots.jumpSpotsTransforms.Length; ++i)   // search all jump spots for the nearest one
                {
                    distToJumpSpot = Vector2.Distance(transform.position, jumpSpots.jumpSpotsTransforms[i].position);
                    if(distToJumpSpot < minJumpDist)
                    {
                        minJumpDist = distToJumpSpot;
                        nearestJumpSpot = jumpSpots.jumpSpotsTransforms[i];
                    }
                }
                Debug.Log("The nearest jump spot is " + nearestJumpSpot.name + " at " + nearestJumpSpot.position);
            }
        }
    }

I don’t see anything in that code that would lock in the first answer, although if all spots are farther than 99 away that would cause problems. On line 22 you might want to set minJumpDist to a ludicrously large number just to be safe.

It seems a bit odd that I don’t see anything that would restrict the search of jumpSpots to only spots on the enemy’s current platform level. Also, it’s a bit worrisome that you are using Vector2.Distance but passing it Vector3s as arguments; IIRC that means the Z coordinate is being ignored, which might be what you want, but you should be careful.

You might try logging transform.Position and the various distances that you calculate to check them for sanity. When you “move the enemy”, did that successfully move whatever game object this script was attached to? Do the distances look reasonable?

Debug.Log. Put it everywhere. Print out the “obvious” values. Question your assumptions. You will find one or more of them to be incorrect. Maybe your jumpSpots list is not populated. Maybe all of your jump spots are in the same place. Maybe your script is not actually on the object you’re moving around but a different object that is staying still.