# Army formation

Updated on May 27, 2018 in
21 on May 22, 2018

How hard is it to make a army formation that travels in formation to where you click with your mouse. Also being able to select individual soldiers and move them. Is this too hard? Or is it simple?

• Liked by
on May 22, 2018

That depends on what your formation is.

If it’s something like a square (rectangle) or a circle, then it wouldn’t be too hard, because you can extrapolate individual position from the shape with simple math. Same thing for a straight line.

More complicated shapes like maybe a semi-circle, or a /\ arrow formation, may take some extra work, but they shouldn’t be totally unreasonable for anyone to make.

Same thing if you want the shape filled with troops. Simple shapes would take simple math, just more of it.

• Liked by
on May 23, 2018

Just a square, I’m new to coding about a year, however I have only started Unity this month so I’m wondering if it’s too difficult for a beginner like me? Also how would I execute this?

on May 23, 2018

You can use pathfinding and let one soldier be the middle of the formation. Then get the size of your army that you want to move and calculate these points around the one soldier in the middle, then update the positions everytime the middle soldier moves?

• Liked by
on May 24, 2018

I’m having trouble with the maths could you explain?

Wise
on May 24, 2018

Can we see what you have so far?

on May 24, 2018

Well, all I have done so far is so wherever you click the cube goes using a nav mesh. But when I began thinking about how to do the math I need a bit of a push to start.

Wise
on May 24, 2018

Well, then before jumping right into making shapes, lets try just getting a line of cubes to move together.

```public class Soldier : MonoBehaviour
{
public float speed;
public Vector3 destination;
void Move()
{
// do something
}
}

public class EliteSoldier : Soldier
{
public List<Soldier> squad = new List<Soldier>();
}
```

What we have here is a normal soldier, and the squad leader (elite)

Both have a marching speed (speed), a destination they’re marching to (destination), and a method to move them. EliteSoldiers however, get to have a squad of other soldiers (that can contain other elites, but you can’t access their squad, because they’re not recognized as an elite).

From here, you would feed elite soldiers the destination you want the squad to move to, and then they would tell all the soldiers in their list where to move. The position they tell the squad is based on their position, and the shape you’re trying to make.

example: you have number of soldiers and want them to be in a straight line with the elite in the center.

```for (int i = 0; i < n; i++)
{
int rightLeft = (i%2==0)? 1:-1; // if the grunt should be on our right(1) or left(-1)
int offset = (i/2) + rightLeft; // I don't remember if ints round up or down =/
squad[i].destination = destination + (transform.right * offset);
}
```

since the squad follows the leader, we can just set their destinations, to the leaders destination + whatever math our shape can be represented by.

• Liked by
on May 25, 2018

Thank you so much, how would I make it so that the elite soldier is the first one you select

• Liked by
on May 26, 2018

The soldiers just vibrate and move weirdly when in a line also how would I make this a square (idk much math)

Wise
on May 26, 2018

area of a square is equal to the product of the  the number of columns and the number of rows.

A = col * row

```for(int c = 0; c < numberOfColumns; c++)
{
for(int r = 0; r < numberOfRows; r ++)
{
Vector3 positionInSquare = new Vector3(c, 0, r);
}
}
```

Also, if you plan on getting into programming, you should probably learn as much math as you can. Programming is 99% math…

and 1% magic.

on May 26, 2018

Yeah, I am starting to learn more complex maths. When the soldiers reach their destination it just vibrates and moves around eachother

Wise
on May 26, 2018

What’s their stopping radius set to?

on May 26, 2018

0.5

on May 26, 2018

Im pretty sure that it might be in the fact that I ended up not using your code entirely because It was causing errors. This is the code I cam up with, I just had a game object which is the leader

```// If not in build mode and the mouse is clicked
if (Input.GetMouseButtonDown(0) && BuildMode.buildMode == false)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
for (int i = 0; i < soldierCount; i++)
{
int rightLeft = (i % 2 == 0) ? 5 : -5; // if the grunt should be on our right(1) or left(-1)
int offset = (i / 2) + rightLeft; // I don't remember if ints round up or down =/
Soldier.destination = hit.point + (transform.right * offset);
}
}
}
```
• Liked by
on May 26, 2018

Could you break down the right left equation to help me understand it better?

Wise
on May 26, 2018

That’s a ternary operation. It’s basically a shorthand if statement.

it takes in a comparison just like an if statement would, in this case (i % 2 == 0)

this is true if i is even, and false if i is odd.

the 2 numbers after that are the results for each.

for your current code, that would be 5 if it returns true, and -5 if it returns false.

on May 26, 2018

I think I’ve figured out the problem. So do I put the elite soldier class in the movement script for the leader but not in the other regular soldiers? If so how do I access the squad and destination variable from another script without making it static?

Wise
on May 26, 2018

no. the movement is in the regular soldier class. the elite inherits from the regular soldier, thus also inheriting the movement ability.

Both regular and elite soldiers need the ability to move.

you access the squad through the elite soldier, since all elites would have their own unique squads, and you access the squad’s destinations through there too.

nothing needs to be static

on May 26, 2018

It still says I can’t access a nonstatic field.

on May 26, 2018

Wait sorry if this is frustrating you but this is my code.

```public class Soldier : MonoBehaviour
{
public NavMeshAgent soldier;
public Vector3 destination;
public float Offset = 10f;
public int soldierCount;
private void Start()
{
soldierCount = 4;
soldier = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
// If not in build mode and the mouse is clicked
if (Input.GetMouseButtonDown(0) && BuildMode.buildMode == false)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
for (int i = 0; i < soldierCount; i++)
{
destination = hit.point;
int rightLeft = (i % 2 == 0) ? 1 : -1; // if the grunt should be on our right(1) or left(-1)
int offset = (i / 2) + rightLeft; // I don't remember if ints round up or down =/
squad[i].destination = destination + (transform.right * offset);
}
}
}
}
}
public class EliteSoldier : Soldier
{
public List<Soldier> squad = new List<Soldier>();
}
```

Wise
on May 27, 2018

That’s because you’re trying to access it wrong. The regular soldier class handles movement. That’s it. Not input, not setting the formation, not tracking their offset. Just set their destination, and let the navmesh do its thing.

The elite soldier is the one that get to take input, and set the formations. You only ever access squad from the elite, not the normal soldiers. They don’t care about the squad. Only themselves.