• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How i can cancel a path with AI enemy?

E

etiboogi

Guest
Hello guys,I need help to cancel the path when the enemy will detect the player.I searched on the internet but I don't find...So can you help me.

this is my script:

//(CREATE)

TargetX= 0;
TargetY= 0;

//Paths

path_start(PathEnemies,1,path_action_restart,true);




//(STEP)

TargetX = oDino.x - x;
TargetY = oDino.y - y;

var _targetX = sign(TargetX) * Speed;
var _targetY = sign(TargetY) * Speed;

// x


if (distance_to_object(oDino) < 160)
{
if(place_meeting(x+_targetX, y, oWall)){
while(!place_meeting(x+sign(_targetX), y, oWall)) {
x += sign(_targetX);
}
_targetX = 0;
}
x+=_targetX;

//y

if(place_meeting(x, y+_targetY, oWall)){
while(!place_meeting(x, y+sign(_targetY), oWall)) {
y += sign(_targetY);
}
_targetY = 0;
}
y+=_targetY;


///path direction

if (direction >= 306 or direction <= 45)
{
sprite_index = sEnemiesL;
image_xscale = -1;
}

if (direction >=46 and direction <= 135)
{
sprite_index = sEnemiesU;
image_xscale = 1;
}


if (direction >=136 and direction <= 225)
{
sprite_index = sEnemiesR;
image_xscale = 1;
}


if (direction >=226 and direction <= 305)
{
sprite_index = sEnemiesD;
image_xscale = -1;
}
}
 
If the player and enemy characters are all on paths, then using:

path_num = path_get_number(path_name) - 1;
path_point = path position * path_num;

gives a reasonable idea of which path point number they're at. That's if you've got a path that's been calculated using mp_grid_path, or at least one where you have a point for each step they make as they travel it (I don't build my paths like that, and only use a start and end point)

Then you'd want a for loop, that begins at 'path_point' and ends at 'path_num'. This example doesn't use sprite based collisions, but instead approximates the size of the object as it moves through the path. Doing the same for the enemy too, then using rectangle_in_rectangle to see if they collide.

Code:
num = instance_number (obj_enemy) - 1;
width = sprite_width / 2;  // this is assuming your checking object has a centered origin, and is giving it "mass". As this doesn't check sprite collisions
height = sprite_height / 2; //  ^^^^^
collision = false;
for (i = path_point; i < path_num; ++i) // loops through remaining path points
{
var px, py, left, right, top, bot;
px = path_get_point_x (path_name, i);
py = path_get_point_y (path_name, i) ;
left = px - width;  // defining left side
right = px + width; // right side
top = py - height; // top side
bot = py + height; // bottom side
for (ii = 0; ii < num; ++ii)  // loops through enemy instances
{
var enemy, enemy_path, en_width, en_height, en_path_num, en_path_point, cur_path_point, en_px, en_py, enemy_left, enemy_right, enemy_top, enemy_bot;
enemy = instance_find(obj_enemy, ii);
enemy_path = enemy.path_name;
en_width = enemy.sprite_width / 2;
en_height = enemy.sprite_height / 2;
if path_exists(enemy_path) // if enemy is on path
{
en_path_num = path_get_number(enemy_path) - 1; // their total path number
en_path_point = enemy.path_position * en_path_num; // their current path point number
cur_path_point = en_path_point + i; // ^^^^ with i added to move it forward
if cur_path_point <  en_path_num  // is less than total path number?
{
en_px = path_get_point_x (enemy_path, cur_path_point);
en_py = path_get_point_y (enemy_path, cur_path_point) ;
}
else             // just uses end path point
{
en_px = path_get_point_x (enemy_path, en_path_num);
en_py = path_get_point_y (enemy_path, en_path_num) ;
}
}
else // enemy is not on path, so just uses their x / y
{
en_px = enemy.x;
en_py = enemy.y}
enemy_left = en_px - en_width;
enemy_right = en_px + en_width;
enemy_top = en_py - en_height;
enemy_bot = en_py + en_height;
if rectangle_in_rectangle (left, top, right, bot, en_left, en_top, en_right, en_bot)
{
collision = true;
break; // breaks out of instance checking loop
}
}
if collision
{
break;  // breaks out of path checking loop
}
}

if collsion
{
path_end()
path_delete(path_name)
etc....
}
 
Last edited:
Top