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

Get help using Construct 2

» Tue Apr 14, 2015 2:26 pm

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?
B
3
Posts: 4
Reputation: 183

» Tue Apr 14, 2015 3:35 pm

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.
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

» Tue Apr 14, 2015 3:46 pm

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.
B
3
Posts: 4
Reputation: 183

» Tue Apr 14, 2015 5:17 pm

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 told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

» Tue Apr 14, 2015 8:25 pm

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...
B
3
Posts: 4
Reputation: 183

» Tue Apr 14, 2015 11:35 pm

Here you go:

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

Just a capx with exactly what I said above.

I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

» Wed Apr 15, 2015 11:11 am

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
B
3
Posts: 4
Reputation: 183

» Wed Apr 15, 2015 11:17 am

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
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

» Wed Apr 15, 2015 11:24 am

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.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775