# Basic 2D Enemy AI

This is the basic layout of my game: http://www.youtube.com/watch?v=qcd-CcKWjGU&feature=youtu.be

To make him move I just made it so you click left or right to move, just for a test.

Anyway, I just need an idea on what I need to do to the enemy follow the player. I'm not sure what variables I need and if I need some sort of angle variable so he can move in all possible direction. I probably just want to start out by making it follow the player on the x direction and then start making him move towards the player. This is my first time really having ago at proper AI as the last game I made invlolving AI what just making the enemy move right and turn left once reaching a certain point and vice versa.

This is all really have so far:

rectangle = new Rectangle(
(int)position.X,
(int)position.Y,
texture.Width,
texture.Height);

float et = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

position += velocity;

if (InputManager.Instance.KeyDown(Keys.Left))
velocity.X = -3f;
else if (InputManager.Instance.KeyDown(Keys.Right))
velocity.X = 3f;

if (velocity.X > 0)
facingRight = true;
else if(velocity.X < 0)
facingRight = false;//This is just so that the enemy will look in the right direction of the way he's moving.


I also have an origin which is a Vector2 of the enemy's centre which I feel I may need.

• Is the code above the logic for the player character or for the enemy? Commented Nov 25, 2013 at 20:28
• Enemy, but the only logic that will probably stay are the facing Right if statements, rectangle init and the elapsed time. Commented Nov 25, 2013 at 22:15
• There doesn't really seem to be a question here (no question marks are a bad sign). Anyway if you want to design AI you should abstract the movement and the AI logic from each other. If the enemy and player are the same general thing they can share movement logic and the player will have input logic and the ai will have decision making logic. Basically you just need to figure out what you want your AI to do, then have it tell the movement logic how it should be behaving. For example if you want to go to the player, find which direction the player is in and tell movement to go that direction. Commented Nov 26, 2013 at 23:08

When there are no obstacles, you just need to check if the enemy is left or right, above or below the player-character. You can do so by comparing their X- and Y- coordinates. Then move the enemy in that direction.

When there are obstacles and you want your enemies to find a path around them, you will need a route-finding algorithm like A*.

• I have this: imgur.com/zxOn1vg But as you would expect, once the enemy has reaches the same X pos, it turns left and right really fast. If it reaches the same Y pos, it bounces up and fast. I tried this: if (Position.X == p.Position.X) velocity = Vector2.Zero; But it didn't do anything and I can't think of what else to do :( Commented Nov 26, 2013 at 18:49
• @DanScott When the distance between enemy and player is too small, shouldn't the enemy do something else than moving? Like damage the player? Commented Nov 26, 2013 at 21:50
• That's what I intend do but the enemy has a fit when his x and y coords are the same as the player or just the x or y are the same. I tried doing the above if statement to stop this but it didn't work. Commented Nov 27, 2013 at 15:29
• Never check floating-point values for equality. They have a limited precision, so there is always a certain uncertainty to them. 3.0 might be unequal to 1.0 * 3. Only check them for being in a certain range, like being smaller than playerX + 3.0 but larger than playerX - 3.0. Commented Nov 27, 2013 at 15:37

This should do the job, it will follow the player and face the player as well. Hope this helped.

using UnityEngine;
using System.Collections;

public class ChasePlayerAI : MonoBehaviour {

public Transform target;//set target from inspector instead of looking in Update
public float speed = 3f;

void Update() {
//rotate to look at the player
transform.LookAt(target.position);
//correcting the original rotation
transform.Rotate(new Vector3(0,-90,0),Space.Self);

//move towards the player if distance from target is greater than 1
if (Vector3.Distance(transform.position,target.position)>1f) {
transform.Translate(new Vector3(speed* Time.deltaTime,0,0) );
}
}
}


As Philipp stated, you just have to compute a vector from the enemy to the hero and move the enemy in that direction. That's how you do it when there are no obstacles:

// heroPos is the position of the hero.
Vector2 direction = position - heroPos;

//assuming here that velocity is a length and not a vector.
position += direction*velocity;

if (direction.X > 0)
facingRight = true;
else if(direction.X < 0)
facingRight = false;