Enemy to Follow Player

Hi Monogamers,

I am looking for some help regarding having enemies follow player when the enemy gets to a certain radius.

what would be the best way to implement a simple AI for the enemy class? or more like when the player is in range of enemy, enemy will follow player for x amount of time/distance?

Here is my enemy class:
`
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rpg
{
class Enemy
{

    private Vector2 position;
    protected int health;
    protected int speed;
    protected int radius;

    protected int state;

    public static List<Enemy> enemies = new List<Enemy>();

    public int Health
    {
        get { return health; }
        set { health = value; }

    }

    public Vector2 Position
    {
        get { return position; }
    }

    public int Radius
    {
        get { return radius; }
    }

    public Enemy(Vector2 newPos)
    {
        position = newPos;
    }

    public void Update(GameTime gameTime, Vector2 playerPos)
    {
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;


        Vector2 moveDir = playerPos - position;
        moveDir.Normalize();
        //position += moveDir * speed * dt;
        distanceToPlayer = ;
        int state = 0;

        if (state == 0)
        {
            // Everything the enemy does during "idle"
            if (distanceToPlayer < 600)
            {
                state = 1;
            }
        }
        else if (state == 1)
        {
            // Everything the enemy does during "following"
            position += moveDir * speed * dt; // move enemy towards player
        }

    }
}

class Snake : Enemy
{
    public Snake(Vector2 newPos) : base(newPos)
    {
        speed = 160;
        radius = 42;
        health = 3;

        //state = 0;
    }
}

class Eye : Enemy
{
    public Eye(Vector2 newPos) : base(newPos)
    {
        speed = 80;
        radius = 45;
        health = 5;

        //state = 0;
    }

}

}
`

If the enemy should follow for a specific amount of time, just increment a variable with elapsedseconds in the update method and change the state when the threshold is reached. if state is changed to attack reset that to zero.

Not sure if that’s the best way, but it is a very simple way to accomplish your goal (and as it’s only a single variable more it’s easy for saving the state as well.

more complex solution exist, but I am not sure if you’re unsatisfied with your AI or just your implementation.

I would definitely suggest implementing a very simple state machine for your enemies, it’ll take a little more time than simply keeping track of a following time variable, but it will be a lot more scalable later when you’re doing more complex things.

You should be able to find a ton of references for state machines with some searching, but the general idea would be that the Enemy class keeps some kind of EnemyState object, and the Enemy Update function would simply call the state update function. From your example, states would be something like:

Patrol - Simply checks if the player is close enough. If so, switch to Follow state
Follow - Move closer to the player. If the follow time has elapsed, or if the player has gotten too far away, return to Patrol.
Combat - Not sure if this would be required for your game, but could transition to this if it catches the player. Handles attacking / defending / whatever.

All these states would subclass from the base EnemyState and would have an Update function and probably an OnEnter and OnExit to do whatever kind of initialization / cleanup that needs done upon switching states.

From a glance your code looks good, and should give you the behavior your after. Got a basic state machine, distance check, calculate the velocity correctly.

Is the issue just to follow for a period? Keep some variable (whether distance or time) and increment as @reiti.net suggests, and change back to idle state after some threshold.

I see you didn’t calculate the distance but its basically the same as the velocity above, would be Math.Abs(playerPos - targetPos) or can calculate euclidean distance for whatever dimension vector.