Game Mechanics (SOLVED) Enemies Following Player -- Is There a Better Way?

G

guitarmonkeys14

Guest
Hey everyone,

I am currently making a top down shooter in which I have successfully made the enemy always follow the character. I have hit the point where I feel comfortable enough figuring out my own ways of making everything happen.

Initially I wanted to do something along the lines of an If obj_player exists, but I could find anything along those lines with Drag and Drop. So, I decided to link the enemies movement to whether the player had HP or not, I just wonder if this is the best and most efficient way.

This causes a script error after my player is killed, but I think that is because the enemies have nothing to point towards. I don't think this will be an issue down the line because I will have a respawn/restart feature shortly.

Code:
// If Variable
with(obj_player) var l0C47E57A_0 = HP > 0;
if(l0C47E57A_0)
{
   // Set Point Direction
   direction = point_direction(x, y, (obj_player.x), (obj_player.y));

   // Set Instance Rotation
   image_angle = point_direction(x, y, obj_player.x, obj_player.y);

   // Set Speed
   speed = 1;
}
upload_2019-1-27_18-49-38.pngupload_2019-1-27_18-50-38.png

Ideally I would like to find a way to make the enemy continue straight down mindlessly when the obj_player is destroyed.

Suggestions please??
 
G

guitarmonkeys14

Guest
Well, after about two hours of trial and error I answered all of my questions..

Why would I link to HP when a distance to object exists???

And I figured out making the enemy continue down after player destruction


For anyone out there who may be having trouble

a) getting enemies to follow your player and
b) deciding how they react if your character dies

This is what I did

Code:
if(distance_to_object(obj_player) > 2000)
{
   image_angle = 270;

   direction = 270;

   speed = 1;
}

if(distance_to_object(obj_player) <= 2000)     // set to 2000 (greater than max game size, plus some because enemies spawn outside of room) - I want my enemies always following the player, the only time they don't is if player is destroyed
{
   image_angle = point_direction(x, y, obj_player.x, obj_player.y);     // To make the enemy face the player at all times

   direction = point_direction(x, y, (obj_player.x), (obj_player.y));     // So that when speed is applied it moves towards the player

   speed = 1;     // Change this anywhere from (.001 - 1000) to customize speed
}
Hope this helps my fellow n00bs ;P

For you experienced guys out there, please feel free to correct/tweak/advise wherever possible ;)
 
Last edited by a moderator:

Miradur

Member
Hi, you know the command instance_exists(object)?

Code:
if instance_exists(obj_Player)
   {
   do something ....
   } else 
   Enemy goes down ....
   }
There are also lots of commands to work with objects, so say goodbye to the idea of using their ID as a number.


Miradur
 
Yes, do not ever type out the object ID as a way to reference an object. This is terrible coding practice. Use the functions that are designed to return the object number, rather than hard-coding them (also, make sure you are paying attention to the difference between an object ID and an instance ID...If you have more than one instance of an object in a room, NEVER use the object ID to reference it, always get the instance ID using any of the multiple functions that return an instance ID).
 
G

guitarmonkeys14

Guest
Hi, you know the command instance_exists(object)?

Code:
if instance_exists(obj_Player)
   {
   do something ....
   } else
   Enemy goes down ....
   }
There are also lots of commands to work with objects, so say goodbye to the idea of using their ID as a number.


Miradur
Exactly what I was looking for, thank you so much!
 
Top