• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

How do I make instances of an object follow another object in a certain formation

In a few games that I will make soon, some of them will have a ship with attack drones following it (either the player or a boss), and how do I program that drone object to have its instances follow a different object? Here's an example in this GIF:

ezgif-1015551728.gif

Moderator Note: topic moved to the Programming forum.
 
Last edited by a moderator:

joakimFF

Member
something like this should work,

create event of ship

Code:
drone = instance_create(x+lengthdir_x(50,image_angle -180),lengthdir_y(50,image_angle - 180),obj_drone);

drone.owner = id;
then in drone step

Code:
if instance_exists(owner) {move_towards_point(owner.x + lengthdir_x(50,owner.image_angle -180),owner.y + lengthdir_y(50,owner.image_angle -180),speed);}
 

Genetix

Member
That or using paths, depending on how you feel about them. Once you start using the GML for pathing, they can actually be pretty flexible.
 

Let's Clone

Member
This can be done by having your drones point to a target object (being the ship or boss). Simply have them move towards that location, keeping in mind that you probably want them to stop following when they are close enough.

From there you can chance whatever that target object is and have them begin tracking their new target.

I hope this isn't too vague.
 
U

Uberpink

Guest
This can be done by having your drones point to a target object (being the ship or boss). Simply have them move towards that location, keeping in mind that you probably want them to stop following when they are close enough.

From there you can chance whatever that target object is and have them begin tracking their new target.

I hope this isn't too vague.
how do you make them slow down and stop lets say 50 piksels away from the main object they follow? (if main object slow down or stop)
 
Last edited by a moderator:

NightFrost

Member
Nice necro. Anyway, in the sample gif OP has posted, the drones appear to slavishly follow the exact path player ship has taken, and I suppose they also only move when player moves. This is easily accomplished by keeping a list of player ship's previous positions - let's say twenty previous positions. Every frame you add player's previous position to the top of the list, and if the list is longer than twenty, pop off the bottom one. The first drone always reads its position from ten steps into the list, and the second, twenty steps. This way the drones are always ten and twenty steps, respectively, behind the player.
 
B

Box Face Friend

Guest
Have you thought about making a distance variable for slowing them down?

var dis = (x - obj_player.x);
if (dis > 120) vsp = -1;
else if (dis < -120) vsp = 1;

Im not sure what your drones use to move, but I used something similar to the above to have my enemies stop when they got close to the player.
 
Top