Simple Follow the leader. 1 leader and many followers.

Hi all, I could use some help. I am trying my best, but my BRAIN is rubbish right now. I need help with a very simple follow simulation: I have 40 cubes and 1 capsule. THe Capsule is my leader agent. THe cubes are his minions if you will. What I need help with is when the leader moves the minions follow to certain point and then stop behind the leader. They should also face his back. Much like a general leading his troops. This is the closest I have gotten: PLEASE PLEASE! I need help. By the way if, there’s a much better way than this script (found on forum) I would appreciate it. I dod not need anything as complex as Unity Steer. Think Pikmin. I’m sure many people could use the solution once it’s available. Thanks to All and Unity!

PS: COuld the followers be in an Array? This way I don’t have to add all the prefabs in the physical scene. The code can spawn them. Thanks

using UnityEngine;
using System.Collections;

public class pikminFollow : MonoBehaviour {

public Transform target;
public float smoothTime = 0.3f;
public float distance = 2.5f;
public float correction = 0.1f;
private Vector2 velocity;
private Vector3 thisPosition;
private Transform thisTransform;

void Start ()
{
    thisTransform = transform;
}

void Update ()
{
    // Look at target
    Vector3 targetRelativeDirection = target.position - thisTransform.position;
    targetRelativeDirection.y = 0;

    thisTransform.forward = targetRelativeDirection;

    // Seems to work fine up to here...

    Debug.Log("value 1 - " + targetRelativeDirection.magnitude);
    Debug.Log("value 2 - " + (distance + correction));

    // Follow target if distance between me and target is 
    if (targetRelativeDirection.magnitude > distance + correction)
    {
       Debug.Log("FOLLOWING");

       thisPosition = thisTransform.position;

       thisPosition.x = Mathf.SmoothDamp(thisTransform.position.x, (target.position.x - (distance * targetRelativeDirection.normalized.x)), ref velocity.x, smoothTime);
       thisPosition.z = Mathf.SmoothDamp(thisTransform.position.z, (target.position.z - (distance * targetRelativeDirection.normalized.z)), ref velocity.y, smoothTime);

       thisTransform.position = thisPosition;
    }
    else
    {
       Debug.Log("NOT FOLLOWING");
    }
}
}

Do you want the followers to trace the path the leader took in single file or do you want them just to get as close as they can to the leader by the shortest route? If they just need to take the shortest route then do they need to avoid obstacles along the way?

Putting the followers in an array would definitely seem to be a good idea. You could populate the array by instantiating copies of a prefab or maybe by choosing from a few prefabs at random (if you want a more realistic-looking group where the members don’t all look the same).

I was going to worry about the obstacle avoidance later. Yes, they would need that. I would use larger collides to help with separation. No, they should not follow only in a straight line. More like a scattered bunch following the leader. Could then add later button to make them single file. Really just tying to keep it simple and nice. Can you help with just getting the basic group in the array to follow. With a staggered movement? Thank you!

You might find it easier to do this using a CharacterController on each follower object. You can use its SimpleMove function to make the object move toward the target along the direction vector. The main advantage of CharacterController objects is that they should not end up overlapping or on top of each other (they will block each other’s movement as appropriate). If you use quite a wide collider for the CharacterController, as you mentioned, then it should ensure good spacing between the followers.

As and when you need to add obstacle avoidance, you probably want to look at Unity’s pathfinding system or check out one of the third party solutions on the asset store.

As usual thanks andeee. I started down that path. Then I found a nice little script for 10 bucks on asset store that does this. It’s not meant for iOS, but I can work from there. Thanks again!