- Home /
3*3 Grid GameObjects
I have a script that creates a 3*3 grid of quads on the x & z axis. Example:
[0,0][0,1][0,2]
[1,0][1,1][1,2]
[2,0][2,1][2,2]
1,1 is the original quad and the centre starting position. The other quads are instantiated at runtime.
As the player object moves from lets say 1,1 to 1,2 on the x axis. I want the left column of the 3*3 grid to reposition to become the right column Example:
[0,0][0,1][0,2]
[1,0][1,1][1,2]
[2,0][2,1][2,2]
Would become:
[0,1][0,2][0,0]
[1,1][1,2][1,0]
[2,1][2,2][2,0]
Although with my current code instead of the player object starting over the top of 1,1 in the centre, the entire grid is pushed left on the x axis and the player starts on top of 1,2 (the right column of the grid)
Here is the code I use. I adapted another script which was designed for use with Unity Terrain instead of objects. Here is the link to the video with the script in the description: https://www.youtube.com/watch?v=cUAprBYS_0Q
public GameObject playerObj;
private GameObject[,] environmentGrid = new GameObject[3,3];
private int environmentLevelSize = 10;
public GameObject environmentObj;
void Start ()
{
environmentGrid [0, 0] = (GameObject)Instantiate (environmentObj);
environmentGrid [0, 1] = (GameObject)Instantiate (environmentObj);
environmentGrid [0, 2] = (GameObject)Instantiate (environmentObj);
environmentGrid [1, 0] = (GameObject)Instantiate (environmentObj);
environmentGrid [1, 1] = environmentObj;
environmentGrid [1, 2] = (GameObject)Instantiate (environmentObj);
environmentGrid [2, 0] = (GameObject)Instantiate (environmentObj);
environmentGrid [2, 1] = (GameObject)Instantiate (environmentObj);
environmentGrid [2, 2] = (GameObject)Instantiate (environmentObj);
environmentGrid [0, 0].name = "[0,0]";
environmentGrid [0, 1].name = "[0,1]";
environmentGrid [0, 2].name = "[0,2]";
environmentGrid [1, 0].name = "[1,0]";
environmentGrid [1, 1].name = "[1,1]";
environmentGrid [1, 2].name = "[1,2]";
environmentGrid [2, 0].name = "[2,0]";
environmentGrid [2, 1].name = "[2,1]";
environmentGrid [2, 2].name = "[2,2]";
UpdateEnvironmentPositions();
}
private void UpdateEnvironmentPositions ()
{
environmentGrid [0, 0].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x - environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z + environmentLevelSize);
environmentGrid [0, 1].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z + environmentLevelSize);
environmentGrid [0, 2].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x + environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z + environmentLevelSize);
environmentGrid [1, 0].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x - environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z);
environmentGrid [1, 2].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x + environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z);
environmentGrid [2, 0].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x - environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z - environmentLevelSize);
environmentGrid [2, 1].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z - environmentLevelSize);
environmentGrid [2, 2].transform.position = new Vector3 (
environmentGrid [1, 1].transform.position.x + environmentLevelSize,
environmentGrid [1, 1].transform.position.y,
environmentGrid [1, 1].transform.position.z - environmentLevelSize);
}
void Update()
{
Vector3 playerPos = new Vector3 (playerObj.transform.position.x, playerObj.transform.position.y, playerObj.transform.position.z);
GameObject playerEnvironmentObj = null;
int xOffset = 0;
int yOffset = 0;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if ((playerPos.x >= environmentGrid[x,y].transform.position.x) &&
(playerPos.x <= (environmentGrid[x,y].transform.position.x + environmentLevelSize)) &&
(playerPos.z >= environmentGrid[x,y].transform.position.z) &&
(playerPos.z <= (environmentGrid[x,y].transform.position.z + environmentLevelSize)))
{
playerEnvironmentObj = environmentGrid[x,y];
Debug.Log (playerEnvironmentObj.name);
xOffset = 1-x;
yOffset = 1-y;
break;
}
}
if (playerEnvironmentObj != null)
{
break;
}
}
if (playerEnvironmentObj != environmentGrid [1,1])
{
GameObject[,] newEnvironmentGrid = new GameObject[3,3];
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
int newX = x + xOffset;
int newY = y + yOffset;
if (newX < 0)
{
newX = 2;
}
else if (newX > 2)
{
newX = 0;
}
if (newY < 0)
{
newY = 2;
}
else if (newY > 2)
{
newY = 0;
}
newEnvironmentGrid[newX, newY] = environmentGrid[x,y];
}
}
environmentGrid = newEnvironmentGrid;
UpdateEnvironmentPositions();
}
}
Answer by NoseKills · Feb 26, 2015 at 10:09 PM
You have 2 issues in your code. Mainly the if() that checks which grid square the player is in currently. The first one is that you are allowing "equals" in both the high end and low end of your comparison.
(playerPos.x >= ... && // you should take the "=" away from either of these.
(playerPos.x <= ... // Same with .z
With your condition, if the first floor tile goes from coordinate -10 to 0, the middle one from 0 to 10 and the last from 10-20, the player (at center==0) is found to be in the first one already, because (center0 >= -10 && center0
The second problem is that the floor tiles have their pivot at the center, so when you check
(playerPos.x >= environmentGrid[x,y].transform.position.x) &&
(playerPos.x <= (environmentGrid[x,y].transform.position.x + environmentLevelSize))
you're actually checking that the player is at least at the center of the tile, and at most 5 units outside the tile edge.
You can fix these by allowing equaling only in the top or bottom end, and by allowing the player to travel half a tile to any direction from tile center before enforcing swapping of the tiles
float halfEnv = environmentLevelSize / 2f;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if ((playerPos.x >= environmentGrid[x,y].transform.position.x - halfEnv) &&
(playerPos.x < (environmentGrid[x,y].transform.position.x + halfEnv)) &&
(playerPos.z >= environmentGrid[x,y].transform.position.z - halfEnv) &&
(playerPos.z < (environmentGrid[x,y].transform.position.z + halfEnv)))
{