GMS 2 enemy move toward player (GMS2)

KPJ

Member
Hi everyone! I need help. I want the enemy to move toward the player if the player shoots within a certain range. At first, all my code was working and the enemy moved toward the player but then all of a sudden it stopped working and the enemy didn't move.

Code:
//if player shoots
with (oEnemy)
{
    if (distance_to_object(oPlayer) < bulletalertrange)
    {
        placeofinterestx = oPlayer.x;
        placeofinteresty = oPlayer.y;
        estate = estates.chasealert;
    }
}

//Enemy create
grid = mp_grid_create(0, 0, room_width/32, room_height/32, 32, 32);
mp_grid_add_instances(grid, oSolid, false);
path = path_add();

//Enemy step
if (estate == estates.chasealert)
{
    ChaseAlert();
}

//Enemy chase alert script
nearestdoor = instance_nearest(x, y, oDoor);

mp_grid_path(grid, path, x, y, placeofinterestx, placeofinteresty, true);
path_start(path, enemyspd, path_action_stop, true);

if (x == placeofinterestx) && (y == placeofinteresty)
{
    if (alarm[2] == -1)
    {
        alarm[2] = 5;
    }
}

if (distance_to_object(nearestdoor) < 20)
{
    with (nearestdoor)
    {
        if (closing)
        {
            opening = true;
            closing = false;
        }
    }
}
Anyone know what's happening? Thanks!
 

EvanSki

Raccoon Jam Host
path_start(path, enemyspd, path_action_stop, true);
I would say its that line of code here, as if it gets to where the player was at the time of calling the code and the player moves, the enemy will just move to where the player was, not where it is at the given time

I have a tutorial on mp_girds if your interested, you could plug your code into it and it should work
 
  • Like
Reactions: KPJ

KPJ

Member
EvanSki that's how I want it to work. I want the enemy to investigate the players wherabouts at that moment in time. (sorry for not making that clear) The problem is however, the enemy does not move at all. One second it was working perfectly but the next it doesn't even move
 
Last edited by a moderator:

EvanSki

Raccoon Jam Host
EvanSki that's how I want it to work. I want the enemy to investigate the players wherabouts at that moment in time. (sorry for not making that clear) The problem is however, the enemy does not move at all. One second it was working perfectly but the next it doesn't even move
enemyspd,
is your enemyspeed starting at 0? or is that a set speed the enemy can go at any time?
 
Last edited by a moderator:
  • Like
Reactions: KPJ

KPJ

Member
My enemy speed is set to 4.5 from the start (the create event of the enemy). I also noticed something else. If my enemy is behind a wall but within 100 px of the player, that's when it doesn't work. However, if the enemy is within the range and not behind a wall (clear sight of the player) it works. I don't know why though
 

KPJ

Member
Lady Glitch Wow! Thanks so much! I completely forgot that I changed it from oWall to oSolid. It works perfectly now. Thanks for all your help Lady Glitch and EvanSki :)
 
Last edited by a moderator:
Top