Hi all, I was hoping I could get some help on figuring out the maths of my card instantiation function.
I am able to get the appropriate number of cards to spawn in rows and columns like in the picture above, but I’m having trouble figuring out my math for positioning/spacing.
In the image below, my partner has drawn what the client wishes the ‘card board’ to look like; think ‘Guess Who’ board game; with the perspective being dead on.
(Since my image didn’t work, click here for the image)
So I’ve tried to create an instantiation function that starts at the center and subtracts a predesignated float amount for the xGap, then switching over for the second half of the row. Also adding to the scale as they get ‘closer to the camera’.
However, when I add in my math for positioning, I get one less card per row and the positioning seems slightly off… Here’s my code! I hope someone can help simplify/fix this lol
public GameObject blankCard;
private int shuffleNum;
public int rowsTotal = 5;
public int columnsTotal = 8;
public Vector2 gridStartTopLeft = new Vector2(-0.55f, 1.6f);
public float gridGapX = 1.1f;
public float gridGapY = 1.3f;
public float gridPaddingX = 0.1f;
public float scaleMod = 0.1f;
Start () {
for (int r = 0; r < rowsTotal; r++)
{
int c = 0;
int cTmp = 0;
int tmpNum = 0;
bool flip = false;
while (c < columnsTotal)
{
if (!flip)
{
shuffleNum = rnd.Next(0, cardIndexes.Count);
GameObject newCard = Instantiate(blankCard, new Vector2(gridStartTopLeft.x - ((gridGapX * cTmp) - gridPaddingX *r), gridStartTopLeft.y - (gridGapY * r)), Quaternion.identity);
newCard.transform.localScale = new Vector2(1 + (scaleMod * r), 1 + (scaleMod * r));
c++;
cTmp++;
tmpNum--;
}
else
{
shuffleNum = rnd.Next(0, cardIndexes.Count);
GameObject newCard = Instantiate(blankCard, new Vector2(gridStartTopLeft.x + ((gridGapX * cTmp) + gridPaddingX * r), gridStartTopLeft.y - (gridGapY * r)), Quaternion.identity);
newCard.transform.localScale = new Vector2(1 + (scaleMod * r), 1 + (scaleMod * r));
c++;
cTmp++;
tmpNum++;
}
if (tmpNum < -(columnsTotal / 2) +1)
{
tmpNum = 0;
cTmp = 0;
flip = true;
}
}
gridGapX += gridPaddingX;
}
}