#### Rexzqy

##### Member
So i made a rough fleeing code for enemy:

in step event:

if fleeing = true
{
if alarm[4] = -1
{
alarm[4] = room_speed*0.25;
}
}

in alarm[4]:

if mp_grid_path(grid,path,x,y,obj_player.x,obj_player.y,true)
{
path_start(path, -0.5, path_action_stop, false);
}

i thought it would make them go to the opposite direction, but they just teleport right to the player object..Any idea why? Thanks in advance!!

#### TsukaYuriko

##### ☄️
Forum Staff
Moderator
Going in reverse means starting at the end. The end position for your pathfinding is the player. Therefore your start position is the player.

I'm not sure how this code is meant to represent fleeing when all it ever does is make the calling instance move from the player's position to its current position - even after it does that, it will not have moved past where it is currently at. You can't negate "move towards the player" into "move away from the player".

#### Rexzqy

##### Member
Going in reverse means starting at the end. The end position for your pathfinding is the player. Therefore your start position is the player.

I'm not sure how this code is meant to represent fleeing when all it ever does is make the calling instance move from the player's position to its current position - even after it does that, it will not have moved past where it is currently at. You can't negate "move towards the player" into "move away from the player".
Ah, thank you! I thought this will act like mp_potential_step when it has a negative speed..

#### Yal

##### 🐧 *penguin noises*
GMC Elder
How about pathfinding from the player, to some random point further away, and then following that path with a positive speed?

#### Rexzqy

##### Member
How about pathfinding from the player, to some random point further away, and then following that path with a positive speed?
if mp_grid_path(grid,path,x,y,x+lengthdir_x(30,point_direction(obj_player.x,obj_player.y,x,y)),y+lengthdir_y(30,point_direction(obj_player.x,obj_player.y,x,y)),true)
{
path_start(path,0.5,path_action_stop,false);
}

i did something like this and the enemy is finally moving away from the player.. but now i am kinda stuck on what if that exact position or general direction has wall.. maybe i could add a code where if that happens it would pick a new direction?

#### RefresherTowel

##### Member
Tbh, I wouldn’t use pathfinding for this, I think steering behaviours are much more suited to this kind of dynamic avoidance situation.

#### Rexzqy

##### Member
Tbh, I wouldn’t use pathfinding for this, I think steering behaviours are much more suited to this kind of dynamic avoidance situation.
Thank you for the idea but could u specify a bit? Would that be mp potential step and then combine that with some wall avoidance code?

#### Yal

##### 🐧 *penguin noises*
GMC Elder
Try googling something like "steering behavior tutorial" and you might find a more in-depth explanation? (That goes for any cool technical buzzword you don't understand actually)

One cool system I've seen recently is raycasting in all directions* to see which directions are the most "open", and then having a desired direction to move in; the final movement direction you pick is obtained by doing the dot product of the desired direction and all the raycast lengths and pick whatever direction has the highest result (so if the direction is blocked you get a 0 dot product, and if the direction is the opposite direction from your goal you get a negative dot product, but a diagonal that's KINDA in the right way will win out if all "better" options are blocked). When chasing the player, the desired direction is point_direction(x,y,player.x,player.y), when escaping it's that direction + 180 (so you move AWAY from the player), and when the enemy just is patrolling you could just randomly set this to a random direction every 5-10 seconds or something.

Might be a bit too advanced for you though, raycasting means a lot of collision checks and you need to have your own movement systems and stuff instead of the built-in ones. But it's worth considering.

*not literally all 360 directions, but like 8 evenly distributed ones

#### RefresherTowel

##### Member
Sorry, I didn't see the reply, but yeah, @Yal covered basically what I would've said (even included the raycasting I was originally thinking of mentioning but decided not to, lol).

#### Rexzqy

##### Member
Try googling something like "steering behavior tutorial" and you might find a more in-depth explanation? (That goes for any cool technical buzzword you don't understand actually)

One cool system I've seen recently is raycasting in all directions* to see which directions are the most "open", and then having a desired direction to move in; the final movement direction you pick is obtained by doing the dot product of the desired direction and all the raycast lengths and pick whatever direction has the highest result (so if the direction is blocked you get a 0 dot product, and if the direction is the opposite direction from your goal you get a negative dot product, but a diagonal that's KINDA in the right way will win out if all "better" options are blocked). When chasing the player, the desired direction is point_direction(x,y,player.x,player.y), when escaping it's that direction + 180 (so you move AWAY from the player), and when the enemy just is patrolling you could just randomly set this to a random direction every 5-10 seconds or something.

Might be a bit too advanced for you though, raycasting means a lot of collision checks and you need to have your own movement systems and stuff instead of the built-in ones. But it's worth considering.

*not literally all 360 directions, but like 8 evenly distributed ones
Thx for the detailed explanation! I just kinda "cheated" -- checked 8 directions and each I set 3 different distances(lengthdir 50,150,250), if all fail they just quit fleeing and turn into alert/attack mode again. Works ok so far since this enemy only spawns in an area where walls are thin, so most likely they can find a position that's not blocked. Will check out the steering behavior though, seems very interesting and could be very useful in the future!