GameMaker Making enemies detect the player and do damage to it

Kami

Member
Hi, I have a small platformer project, and I´m trying to make progress with it, even not knowing so much of GML.

It turns out I'm trying to make that the enemy recognize when the player is in his front, and if it is, the enemy does damage to it (Not really meeting with it, just stoping and doing a melee attack). I´ve been searching on youtube for a tutorial, but I didn´t find out something like that. If you could help me I would be very grateful. Thanks
 

Slyddar

Member
You need to have a facing type variable on each, which identifies which direction they are facing. I use "facing" and have it set to -1 for facing left and 1 for facing right. If you use that you can then use the player/enemy x position to calculate if they are facing each other. So the enemy would check if the player is within a certain distance, say 100 pixels as below, and then if facing the player, start the attack.
e.g.
Code:
//Enemy STEP event
if (distance_to_object(o_player) < 100) and (sign(o_player.x - x) == facing)  {
  //Attack the player
}
 
Top