Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Follow the leader?

Discussion in 'Scripting' started by brandc, Mar 17, 2015.

  1. brandc

    brandc

    Joined:
    Feb 23, 2015
    Posts:
    16
    I'm trying to get a leader with multiple followers completed and having real issues. I'd like to have the followers to follow the exact path the leader has taken similar to snake. I think I'm over complicating the process as I currently move the leader in a constant direction (Up, Down, Left or Right).

    When I turn the leader it records the position and direction into a List then when a follower hits that point he turns to the new direction. Its 2D so Im using Vector2.

    The codes a mess and its buggy can anyone simplify this for me?

    I want each character to have the ability of having a "party" and his own followers.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Here's what I would do:
    1. Create a "timed position" class. It contains a "position" Vector2 and a "time" float.
    2. In the follower, have a Queue<TimedPosition> object. Every frame, Push the leader's location and the current time.
    3. Also every frame, Pop the last item from this queue; do this until Time.time is >= the item's "time" + followingDelay. Set the follower's position. (usually this will only be 1 Pop per frame, but doing it this way allows it to compensate for changes in framerate)

    Making this a part of your own class would also allow you to add other data to TimedPosition as well, e.g. the current animation playing on the leader.
     
  3. brandc

    brandc

    Joined:
    Feb 23, 2015
    Posts:
    16
    Great i'll try and implement that now thank you.

    How would you layout these components? Each of my characters will have the ability to be controlled by the player if they selected that character as the leader. So multiple game objects and just attach either a controller/leader or follower script at runtime?

    So:

    Player selected Hero1,
    Hero1 is controller by swipe/directional keys,
    Hero1 collides into spawned Hero2,
    Attach follower script to Hero2,
    Hero2 follows Hero1?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Do they have to follow the leader's path exactly? Can you use a steering behavior instead?
     
  5. brandc

    brandc

    Joined:
    Feb 23, 2015
    Posts:
    16
    Yeah I would like the movement to follow the same path, so the followers will continue to move towards where the leader turned then turn on that point. I managed to get following complete with MoveTowards but when the leader turned the follower started off the path.

    Grid based continuous movement if that makes any sense.
     
  6. J0hn4n

    J0hn4n

    Joined:
    May 11, 2015
    Posts:
    24
    The follow leader behavior its easy to make, if you want a simple unit follow the leader(stay behind him) just use the reverse Velocity vector of the leader, that should be the destination of the unit.

    else if you want multiple units go to that point , calculate the center of mass of that point / number of units, and add separation, the orientation of the units should be the Leader direction.
     
  7. J0hn4n

    J0hn4n

    Joined:
    May 11, 2015
    Posts:
    24
    Anyway if you are on doubts only ask.