I'm working on the first formation for my game's troops. These should simply stand on land, facing the same direction.

To achieve this, I tried to create offsets towards a pivot-unit, that is approximately the unit in the center of the formation. The units are (temporary) elements of a squad list withing the Squad class. I divide the List by 2 and get the closest integer, that should be the pivot point of the formation. Now I generate offsets for all units on the left side (Unit 1 to pivot-1) and right (pivot+1 to last unit) based on the distance/gaps.

This is the code for the line formation. Sadly, the units are only forming a very stange line with no visually detectable system:

int count = (int)(squadMembers.Count / 2);
pivot = squadMembers[count];
center = pivot.transform.position;
switch (type) {
    case FormationType.Line:
        int space = 5;
        for (int i = 0; i < squadMembers.Count; i++) {
            Unit tmp = squadMembers[i];
            int dist = 0;
            if (i > count) {
                dist = (count + 1) * space;
                center.x -= dist;
            if (i < count) {
                dist = (count - 1) * space;
                center.x += dist;

It also appears that this only works along the x axis, any idea how i can realize this with the walking/facing direction ov the pivot?

Is there any (obvious) error? How can I improve this script?

enter image description here

Wanted | Current

PS: Any idea, how I can transform this into a double line (2 Lines behind with half spacing)

  • \$\begingroup\$ I think some images would help clarify this question. Image of what you have and an image of what you want. \$\endgroup\$
    – House
    Mar 1, 2017 at 23:52
  • \$\begingroup\$ @Byte56 Added the current image, I think it's not hard do visualize a straight line in this case, is it? \$\endgroup\$
    – pguetschow
    Mar 2, 2017 at 6:05

1 Answer 1


Well I hope I understood your question correctly: you want your squad members at the same distance from one to another, facing the same direction and forming a line.

First I'd tackle the direction issue. Let's say there is a point in world called destination they are aiming to. You may want to create a Vector3 describing the difference between destination and the position of the first member. This will give you the orientation to be aligned to. Once you got that you just have to normalize this vector and multiply it by the distance you want between squad members. This will give you an offset to apply from a member to another. So... adding this offset to the first member position will give you the second member position... and so on.

Once the alignement is not a problem anymore, making your unit members look in the same direction (means, looking at destination point), you can simply use LookAt method on their Transform.

Something like this (edited to use the pivot):

public class Aligned : MonoBehaviour
    private List<Transform> units;
    private GameObject destination;
    private float distance = 5;

    private void Start()
        // Find the ID of the pivot
        // ie: unit at the center of the line / list
        int pivot_id = units.Count / 2;
        Transform pivot = units[pivot_id];
        // Make the pivot look at the destination and save the rotation
        Quaternion look_rot = pivot.rotation;

        // Create the offset between units
        Vector3 offset = pivot.position - destination.transform.position;
        offset = offset.normalized * distance;
        for (int i = 0; i < units.Count; ++i)
            // Apply position offset
            units[i].position = pivot.position + offset * (i - pivot_id);
            // Make the unit look at the destination point
            units[i].rotation = look_rot;
  • \$\begingroup\$ Thanks for the reply; this is relatively close to my first attempt, when the lines was ordered from left to right, but in this cas I'm struggeling with the correct offset if I want to form the line based on a unit in the middle \$\endgroup\$
    – pguetschow
    Mar 2, 2017 at 10:04
  • \$\begingroup\$ Edit: They,re also standing nearly always infront of the pivot unit, instead next to it. \$\endgroup\$
    – pguetschow
    Mar 2, 2017 at 10:25
  • \$\begingroup\$ The offset would be the exact same based on the unit in the middle. Just use position - offset*unit_count/2.0f on the first unit only and it'll do the trick. \$\endgroup\$
    – lvictorino
    Mar 2, 2017 at 12:41
  • \$\begingroup\$ Edit: I'm not sure to understand what the pivot unit is for you :S \$\endgroup\$
    – lvictorino
    Mar 2, 2017 at 12:41
  • \$\begingroup\$ Jup, did changed that already, was on the train while writing this^^ Pivot unit is the unit in the centre everything is relative to within the formation. \$\endgroup\$
    – pguetschow
    Mar 2, 2017 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .