- Home /
How to code a formation of objects for a RTS simulation game?
Hi, I'm trying to make a pyramid formation which look like this for my RTS game
I have already done it by doing it manually via code, but doing so can only be done using an array that have a maximum container of 9 Objects.
What I'm aiming is if I add more and more object the pyramid will become longer and taller using the for
method.
For Example:
If I have 16 Game Objects it will be like this
1
4 x 2
8 6 3 5 7
15 13 11 9 10 12 14
And if I have 25 Game Objects it will be like this
1
4 x 2
8 6 3 5 7
15 13 11 9 10 12 14
24 22 20 18 16 17 19 21 23
Keep in mind that x
is an origin point of the formation a.k.a the first object of the array / list for Ex: Unit[0]
Here's my manually done code for the pyramid formation. As you can see it is only limited till 8 objects, but I want it to be unlimited
for(int i = 0; i < selectedUnits.Count; i++)
{
ObjectStat selected0;
selected0 = selectedUnits[0].GetComponent<ObjectStat>();
if(i != 0)
{
ObjectStat selected = selectedUnits[i].GetComponent<ObjectStat>();
if(i == 1)
{
selected.marker2DIn3DVersion.position = new
Vector2(selected0.marker2DIn3DVersion.position.x,
selected0.marker2DIn3DVersion.position.y + Range[i - 1]);
selected.marker3D.position = new Vector2(selected0.marker3D.position.x,
selected0.marker3D.position.y + Range[i - 1]);
}
else if(i == 2)
{
selected.marker2DIn3DVersion.position = new
Vector2(selected0.marker2DIn3DVersion.position.x + Range[i - 1],
selected0.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected0.marker3D.position.x + Range
[i - 1],
selected0.marker3D.position.y);
}
else if(i == 3)
{
selected.marker2DIn3DVersion.position = new
Vector2(selected0.marker2DIn3DVersion.position.x,
selected0.marker2DIn3DVersion.position.y - Range[i - 1]);
selected.marker3D.position = new Vector2(selected0.marker3D.position.x,
selected0.marker3D.position.y - Range[i - 1]);
}
else if(i == 4)
{
selected.marker2DIn3DVersion.position = new
Vector2(selected0.marker2DIn3DVersion.position.x - Range[i - 1],
selected0.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected0.marker3D.position.x - Range
[i - 1],
selected0.marker3D.position.y);
}
else if(i == 5)
{
ObjectStat selected3 = selectedUnits[3].GetComponent<ObjectStat>();
selected.marker2DIn3DVersion.position = new
Vector2(selected3.marker2DIn3DVersion.position.x + Range[i - 1],
selected3.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected3.marker3D.position.x + Range
[i - 1],
selected3.marker3D.position.y);
}
else if(i == 6)
{
ObjectStat selected3 = selectedUnits[3].GetComponent<ObjectStat>();
selected.marker2DIn3DVersion.position = new
Vector2(selected3.marker2DIn3DVersion.position.x - Range[i - 1],
selected3.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected3.marker3D.position.x - Range
[i - 1],
selected3.marker3D.position.y);
}
else if(i == 7)
{
ObjectStat selected5 = selectedUnits[5].GetComponent<ObjectStat>();
selected.marker2DIn3DVersion.position = new
Vector2(selected5.marker2DIn3DVersion.position.x + Range[i - 1],
selected5.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected5.marker3D.position.x + Range
[i - 1],
selected5.marker3D.position.y);
}
else if(i == 8)
{
ObjectStat selected6 = selectedUnits[6].GetComponent<ObjectStat>();
selected.marker2DIn3DVersion.position = new
Vector2(selected6.marker2DIn3DVersion.position.x - Range[i - 1],
selected6.marker2DIn3DVersion.position.y);
selected.marker3D.position = new Vector2(selected6.marker3D.position.x - Range
[i - 1],
selected6.marker3D.position.y);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I have problems with unity android build 0 Answers
How to make buttons have sound when it is highlighted and clicked? 0 Answers
Dictionary in second script empty 1 Answer