• 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!

GameMaker Problem with enemy chasing player

P

Pedau666

Guest
I have a problem when enemy chases player. When the player is on the ladder, so when he is above the enemy the enemy constantly changes its direction and tries to follow the player which causes enemys sprite to make horrible things on the screen. Do you have any ideas how to repair this?

EDIT: Enemys sprite also breaks up when it is trying to catch the player when he is behind the wall which enemy cant go through because he is constantly walking into the wall and then going to the another side and then again chasing player.

enemy step event
Code:
if global.pause = 0
{
vsp = vsp + grv;
hsp = dir* walksp;


if instance_exists(o_player) && distance_to_object(o_player)<200 &&distance_to_object(o_player)>0{
dir = sign(o_player.x-x)
}



//horizontal collision
if(place_meeting(x+hsp,y,o_solid))
{
    while(!place_meeting(x+sign(hsp),y,o_solid))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
    dir *= -1;
}


x = x + hsp;

//vertical collision
if(place_meeting(x,y+vsp,o_solid))
{
    while(!place_meeting(x,y+sign(vsp),o_solid))
    {
        y += sign(vsp);
    }
    vsp=0;
    if (fearofheights) && !position_meeting(x+(hit_box_w/2)*dir, y +(hit_box_h/2)+8, o_solid)
    {
        dir*=-1;
    }
 
}
y = y + vsp;



//horizontal collision enemy_wall
if(place_meeting(x+hsp,y,o_enemy_wall))
{
    while(!place_meeting(x+sign(hsp),y,o_enemy_wall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
    dir *= -1;
}

//vertical collision enemy_wall
if(place_meeting(x,y+vsp,o_enemy_wall))
{
    while(!place_meeting(x,y+sign(vsp),o_enemy_wall))
    {
        y += sign(vsp);
    }
    vsp=0;
    if (fearofheights) && !position_meeting(x+(hit_box_w/2)*dir, y +(hit_box_h/2)+8, o_enemy_wall)
    {
        dir*=-1;
    }
 
}

//animation
if(!place_meeting(x,y+1,o_solid))
{
    sprite_index = walk;
    image_speed = 1;
}
else
{
    image_speed = 1;
    if(hsp==0)
    {
        sprite_index = stay;
    }
    else
    {
        sprite_index = walk;
    }
}


//animation enemy_wall
if(!place_meeting(x,y+1,o_enemy_wall))
{
    sprite_index = walk;
    image_speed = 1;
}
else
{
    image_speed = 1;
    if(hsp==0)
    {
        sprite_index = stay;
    }
    else
    {
        sprite_index = walk;
    }
}

if(hsp != 0) image_xscale = sign(hsp);
};
enemy create event
Code:
//fearofheights
fearofheights = 1;


//walk
walksp = 1.5;
grv = 1;


//health
hp = 40;


//sprites
hit_box_h = 64;
hit_box_w = 20;
walk = s_enemy4;
stay = s_enemy4;


//shoot
can_shoot = 0;
shoot_time = 0;
dmg = 1;

//points
pnt = 10;


flash = 0;
vsp = 0;
hsp = walksp;
dir = 1;
 

Simon Gust

Member
The code for turning around can be like this:
Code:
if (x < o_player.x - 10) dir = 1;
else
if (x > o_player.x + 10) dir = -1;
with this code you also should be able to get rid of the extra if check for the distance_to_object() function.

This
Code:
if instance_exists(o_player) && distance_to_object(o_player)<200 &&distance_to_object(o_player)>0{
dir = sign(o_player.x-x)
}
to this
Code:
if (instance_exists(o_player) && point_distance(x, y, o_player.x, o_player.y) < 200)
{
  if (x < o_player.x - 10) dir = 1;
  else
  if (x > o_player.x + 10) dir = -1;
}
Btw, point_distance() is faster than distance_to_object():

Edit: regarding your second question. I would always have some sort of cooldown that prevents an enemy from doing ai decisions. Like turning around, jumping over obstacles or moving randomly. Everytime the enemy does said action increase the cooldown by some frames.
 
Top