direction dependent object positioning

Hello, I’ve just started working with Unity and I’m having a hard time figuring out how to do basic direction dependent positioning.

So I’ve set up a simple scene. There’s the leader object and a few follower objects. The leader is given a command to move to a certain location on a field. The other objects follow him and group behind the leader at the destination point. Here’s how it works right now: alt text

But I want it to work like this: alt text

Could anyone suggest how to accomplish this.

Thanks in advance.

There are complex ways to do this and one not-so-complex way (I won’t say “easy” though).

You need to create a series of empties behind the leader in a grid … or some sort of defined area behind him that you’ll walk to.

Parent this area as a child of the leader.

When you move the followers, they need to move to that area or each to one point of the grid (depending on how regular you want the result to be).

By being parented, the area following the leader will respect the direction the leader faces which means that the followers will end up behind him.

Hello, what you need to do is simple. when the player clicks on the position where the leader needs to go create an empty GameObject in this position (let’s call it “marker”), give “marker” the rotation of the leader.ex:

marker.transform.rotation = leader.transform.rotation;

then translate “marker” a little to the back direction

marker.transform.Translate(Vector3.back * yourDsitance);

with that done make the group head to “marker”'s position then destroy it.

groupScript.targetPosition = marker.transform.position;
Destroy(marker.gameObject);

I can think of two easy ways to do this:

First you can make the followers a child of the leader. This will make them stay in the same position relative to the leader, but if the leader rotates, the followers will rotate as well.

The second way is put a script on the followers. Make it find the difference between the leaders position and it’s position at the start. Then set it’s transform every frame to the leader’s position subtracted by the initial difference:

public Transform leader;

private Vector3 difference;

void Start()
{
    difference = leader.position - transform.position;
}

void Update()
{
    transform.position = leader.position - difference;
}

This will cause the follower’s position stay relative to the leader in world space, but it will not rotate with the leader.