- Home /
How Do I Align Game Objects Evenly In 2D Space?
I'm creating a mobile trivia game and am trying to space out the blank character game objects.
I want them to align centrally to the screen. For example the max number of characters on one row is 9. If the word length is 12 then it will fill the whole of row1 and then continue onto row2 and place the extra 3 characters centrally on row2. If the the world length is less than 9 e.g. 5, then I want to display them centrally on row1.
So far they are always offset to the left and if I get it working for a longer word then it doesn't work for shorter words.
This is my for loop for placing the game objects:
//instanciate and position blank character spaces
for (int i = 0; i < blankCharacters.Length; i++)
{
if(currentQuestion.Word[i] == ' ') continue; //leave a gap when there is a space
blankCharacters[i] = (GameObject)Instantiate(blankCharacter);
if(i < BlankCharatersPerRow) //1st row
{
blankCharacters[i].transform.position = new Vector3
(((Camera.main.orthographicSize / BlankCharatersPerRow) + BlankCharacterGap) * (currentQuestion.Word.Length - i ) + 7.5f,
-1,
-1);
}
else //2nd row
{
blankCharacters[i].transform.position = new Vector3
((Camera.main.orthographicSize / BlankCharatersPerRow) * (currentQuestion.Word.Length - i ) + 7.5f,
-1.85f,
-1);
}
}
These screenshots show how the current blank character spaces (small green rectangles) are being placed
Your answer
Follow this Question
Related Questions
Object with many children will not show up in the center although its coordinates are set to 0, 0, 0 1 Answer
An easy way to get the global position from a child object 5 Answers
How to make your object follow your touch position faster or instantaneously??? 2 Answers
Convert Input.mousePosition to RectTransform pivot position 2 Answers
Transform.position not working ??? 0 Answers