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);
}
}
}