Trying to create unit formations
Right now i'm just trying to create a basic box formation...I've been doing it the hard way like this:
void CreateBox(int i, int count)
{
if (i == 1)
{
boxX = 0;
boxY = 0;
}
else if (i == 2)
{
boxX = 1;
boxY = 0;
}
else if (i == 3)
{
boxX = 0;
boxY = 1;
}
else if (i == 4)
{
boxX = 1;
boxY = 1;
}
else if (i == 5)
{
boxX = 2;
boxY = 0;
}
else if (i == 6)
{
boxX = 2;
boxY = 1;
}
else if (i == 7)
{
boxX = 2;
boxY = 2;
}
else if (i == 8)
{
boxX = 0;
boxY = 2;
}
else if (i == 9)
{
boxX = 1;
boxY = 2;
}
else if (i == 10)
{
boxX = -1;
boxY = 0;
}
else if (i == 11)
{
boxX = -2;
boxY = 0;
}
else if (i == 12)
{
boxX = -1;
boxY = -1;
}
else if (i == 13)
{
boxX = -2;
boxY = -1;
}
else if (i == 14)
{
boxX = -2;
boxY = -2;
}
pos = new Vector2(boxX, boxY);
}
What is a formula I can use to achieve the same thing? It is basically a box with points that need to be spaced out in each direction in 1 while stay centered at the point of origin 0,0;
I'm not sure what you are trying to mean. $$anonymous$$aybe you can show some pictures.
If you know the exact value sets of x and y and the number of the sets is not so big, you can create a list of the set like boxPositionsListX and boxPositionsListY which contain all the values inside in order. Then fetch the values by boxPositionsListX[i].
Hmm I'm no longer able to comment down there. Anyway it's not 14 that's just my stopping point as an example it should be possible to do a formula so you don't have to do this that's the whole point of this question "What is a formula I can use to achieve the same thing?" This is more of a mathematic question to do a box formation just like, well, like 99% of the real strategy games out there.. I think Raresh was on the right track but there is no way to pass the data to the destination and even if I did get it to work. That would be 3 total for's inside of one another so it's not really a solution.
Answer by Ziron999 · Sep 16, 2015 at 01:00 PM
The solutions provided are far worse on performance and are not answers to my question. I'm going to close this and have just finished doing it the long handed way which seems to be better for performance anyways because i'm not using a single "for" statement this way. for's inside of for's is not a good idea you never wanna do that.
Could probably what you want lol.
Nested loops can be very handy, there's no reason to claim "you never wanna do that".
If you prefer hardcoded logic in this case: that's what you should try to avoid, not only due to the fact that it will be unmaintainable and hard to extend.
you must not use the profiler much to see just how bad it is in any case no matter what you are doing. if you have to do for's inside of for's I highly suggest finding an alternative. at least until the quantum processor is finished haha
sys12 you should of made that the answer you are the only one who was right on to what i'm looking for!!! The people who are giving you and me -'s obviously have no idea what math is...it ticks me off when people like them have over 100 rep and don't even know this type of stuff when it's crucial to program$$anonymous$$g a good game.
It also showed one other thing....it looks like only a hand full of people will even know the answer to this.
Answer by Raresh · Sep 16, 2015 at 09:40 AM
int distance=1; //distance between the units
int i,j;
int unitsY=4,unitsX=4; //how many units on X and Y
for(i=0;i<unitsY;i++)
{
for(j=0;j<unitsX;j++)
{
boxX = j*distance+originX;
boxY = i*distance+originY;
}
}
this is very close but for some reason with:
float distance = 1.5f; //distance between the units
float y, j;
float unitsY = count, unitsX = count; //how many units on X and Y
originX = -count * 1.5f;
originY = -count / 1.5f;
for (y = 0; y < unitsY; y++)
{
for (j = 0; j < unitsX; j++)
{
boxX = j * distance + originX;
boxY = y * distance + originY;
}
}
ok weird they are now going on top of each other and not going anywhere...
I don't understand what you did with Origin X and Origin Y. They are supposed to represent a position set by you, not whatever you did there. Also is your game 2D or 3D. I think you might want to swap Y with Z
it is 3d and I do already swap y with z...my original code works I think the problem with this is pos = new Vector2(boxX, boxY); is only called one time because it's not in the for's? so it only gets the ending result not each one?
Answer by sys12 · Sep 16, 2015 at 11:39 AM
My approach would be:
void CreateBox(int i)
{
var xList = new List<int>() { 0, 1, 0, 1, 2, 2, 2, 0, 1, -1, -2, -1, -2, -2 };
var yList = new List<int>() { 0, 0, 1, 1, 0, 1, 2, 2, 2, 0, 0, -1, -1, -2 };
pos = new Vector2(xList[i], yList[i]);
}
Not sure what count is for.
ok the reason for I and count is count can help with the origin by dividing the total amount (the count) by 2. I is the currently selected object that I need to assign a point to.
I still don't understand. Do you have some more codes or some pictures?

the red units are moving to the purple squares that's what I've done so far the long hard way and it works but...there must be a formula and more optimized way to do this.
Your answer
Follow this Question
Related Questions
Is Mathf.Infinity compatable with the burst compiled in Jobs? 0 Answers
How do I convert a number and a range to another directly proportional number and range? 4 Answers
Predicting the hit point on X axis, based on vector direction 1 Answer
What exactly does Mathf.InverseLerp() do? 4 Answers
How to know How many times the number was multiplied? 2 Answers