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

Discussion in 'Programming' started by Mitchell Productions, Aug 14, 2016.

  1. Mitchell Productions

    Mitchell Productions Member

    Jul 11, 2016
    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:


    Moderator Note: topic moved to the Programming forum.
    Last edited by a moderator: Aug 15, 2016
    Uberpink likes this.
  2. joakimFF

    joakimFF Member

    Jun 20, 2016
    something like this should work,

    create event of ship

    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

    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 likes this.
  3. Genetix

    Genetix Member

    Jun 22, 2016
    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.
  4. Abrexas

    Abrexas Member

    Aug 10, 2016
    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.
    Uberpink likes this.
  5. Uberpink

    Uberpink Member

    Oct 16, 2016
    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: Dec 11, 2017
  6. NightFrost

    NightFrost Member

    Jun 24, 2016
    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.
    Uberpink likes this.
  7. 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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice