Making the enemy chase player once player enters a room.

J

jsipe2017

Guest
I am creating a game the requires the player to move around a dungeon-like level. When entering a room, I want the enemies in that room to attack the player. For code I have:
GML:
path = path_add();
    
    if (mp_grid_path(global.grid, path, x, y, obj_Player.x, obj_Player.y, 1))
        {
            path_start(path, 4, path_action_stop, false);
        }

The only problem with this is that it makes the enemy chase the player no matter where in the GMS "room" it is in. When I made it so there was no way out of the room, the enemy went through the wall, and attacked my player. Would anybody be able to help me figure out a good code to help the enemy only attack the player when it enters a room within the game. I have been researching but I could only find the general map tracking algorithms.
 
J

jsipe2017

Guest
How would I go about using that code? Putting it in like this?

distance_to_object(50pixels)?

I may need to rephrase that.
 

TRP

Member
step event

if
(distance_to_object(obj_Player) < 500)
move_towards_point(obj_Player.x,obj_Player.y,15)
}

the 500 is pixels away from player obj the 15 is the speed at which your enemy goes toward your player. This will make the enemy smack right into your player and overlap it if your don't set some kind of collision property like


if
(distance_to_object(obj_Player) < 10)
speed=0
}

or something
 
Top