GMS 2 Enemy pathfinding

KPJ

Member
Hi! In my top down game my enemies use a grid system to follow the player and avoid objects. However, while my enemy is chasing the player the way the enemy is facing is very choppy (it glitches between 2-3 different angles). How can I make it so that the enemy smoothly looks toward the direction that it is moving?

Code:
Code:
//oGrid Create event
var cellwidth = 32;
var cellheight = 32;

var hcells = room_width div cellwidth;
var vcells = room_height div cellheight;

global.grid = mp_grid_create(0, 0, hcells, vcells, cellwidth, cellheight);

//Add the things to avoid
mp_grid_add_instances(global.grid, oSolid, false);

//oEnemy Create event
path = path_add();

//oEnemy follow player script
var mx = (oPlayer.x div 32) * 32 + 16;
var my = (oPlayer.y div 32) * 32 + 16;

if (mp_grid_path(global.grid, path, x,  y, mx, my, 1))
{
    path_start(path, enemyspd, path_action_stop, false);
}
Any idea how to do this or whats going wrong? Thanks!
 

jo-thijs

Member
Hi! In my top down game my enemies use a grid system to follow the player and avoid objects. However, while my enemy is chasing the player the way the enemy is facing is very choppy (it glitches between 2-3 different angles). How can I make it so that the enemy smoothly looks toward the direction that it is moving?

Code:
Code:
//oGrid Create event
var cellwidth = 32;
var cellheight = 32;

var hcells = room_width div cellwidth;
var vcells = room_height div cellheight;

global.grid = mp_grid_create(0, 0, hcells, vcells, cellwidth, cellheight);

//Add the things to avoid
mp_grid_add_instances(global.grid, oSolid, false);

//oEnemy Create event
path = path_add();

//oEnemy follow player script
var mx = (oPlayer.x div 32) * 32 + 16;
var my = (oPlayer.y div 32) * 32 + 16;

if (mp_grid_path(global.grid, path, x,  y, mx, my, 1))
{
    path_start(path, enemyspd, path_action_stop, false);
}
Any idea how to do this or whats going wrong? Thanks!
Can't you just do this in the end step event?
Code:
image_angle = direction;
 

KPJ

Member
I want it to be so that, for example if the enemy is moving in a straight line to the right, the image angle will be facing to the right, and if the enemy has to do a turn, the image angle will smoothly rotate around that turn
 

jo-thijs

Member
I want it to be so that, for example if the enemy is moving in a straight line to the right, the image angle will be facing to the right, and if the enemy has to do a turn, the image angle will smoothly rotate around that turn
Oh, so you don't want the enemy to look to the diection it is moving, but to have it smoothly turn to that direction.

In that case you can apply what RefresherTowel hinted at:
Instead of:
Code:
image_angle = direction;
do this:
Code:
imag_angle += clamp(angle_difference(direction, image_angle), -5, 5);
However, that will make the enemy turn a bit late (only after having passed the corner).

If you don't want that, you can try this instead:
Code:
var len = path_get_length(path_index);
var fpos = max(path_position - 30 / len, 0);
var tpos = min(path_position + 30 / len, 1);
var fx = path_get_x(path_index, fpos);
var fy = path_get_y(path_index, fpos);
var tx = path_get_x(path_index, tpos);
var ty = path_get_y(path_index, tpos);
image_angle = point_direction(fx, fy, tx, ty);
 
Top