# How do I make an enemy AI to move only up/down or left/right

0 Favourites
• 9 posts
• I'm making an action RPG, I can't figure out how make the enemy sprite chase the player without making it move diagonally.

Here are some

### horrible

pictures to explain, I want the enemy to move like this

I tried with the bullet behaviour + rotating the sprite towards the player but nope, can somebody help me?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Are you using a tile based system?

if so, the problem could be represented as such:

For the most natural movement, you would want the enemy (green) to pass into each of the tiles which the line passes through.

The distance for both x and y can be found like so:

That can be simplified down to 3:1, 4 times.

If you think about the movement in steps:

start with the direction which is longer, and move that number of tiles in that direction, one tile per step, then move the distance in the other direction, one step at a time.

The closer to 0 one value in the ratio is, the more straight the path of motion. If the lengths of either x or y are prime, then this will result in a path where there is one movement on and and one on y.

• Nope, I think i explained it badly.

The player object obviously moves around, I have set up a Line of sight behaviour, so that when the player is near the enemy it starts chasing it. If I use the pathfinder behaviour the bad guy moves greatly but it stops when it reaches the position where the player was at the start of the pathfinding. If I use bullet + rotation it moves diagonally, the thing I don't want it to do.

However, you got the concept of the movement. I can't figure out how to do it.

• You could try giving the enemy 8 direction behaviour and set it to 4 directions

Give the enemy an instance variable "direction" type string

For each enemy

every second

enemy set variable direction = choose("vertical","horizontal")

enemy has line of sight

-- enemy compare variable direction = vertical

----- player.y < enemy.y

enemy simulate control - up

----- player.y > enemy.y

enemy simulate control - down

-- enemy compare variable direction = horizontal

----- player.x < enemy.x

enemy simulate control - left

----- player.x > enemy.x

enemy simulate control - right

• I tried to do so, apparently it considers only the first two events (so it only moves on X or on Y).

Idk, I really don't know how to do that...

• Here you go:

https://dl.dropboxusercontent.com/u/485 ... onLOS.capx

Just a capx with exactly what I said above.

• Tried it, it kinda works. The only problem is that when the enemy's X/Y is equal to the player's one it starts to shake and wobble, and it stops

• Adding an event to stop that, would stop that.

Maybe this would work even better:

player.x = enemy.x

set direction to vertical

player.y = enemy.y

set direction to horizontal

• Problem with this solution is that there is a chance the x positions are never completely the same, so working with is between values would get better results.