3D grid tetris game problem!
Hi everyone, I'm doing a game base on Tetris idea. But I have a problem with block position. Basically, pivot of parent block is at center of one child block like image A, so all other child blocks will be positive position.
But I want it to rotate around centre on parent block so I move it like image B, so it will have 2 block with negative postion =>> will be wrong in grid check.
Here is my script:
Grid manager script:
public class GameManager : MonoBehaviour { public static GameManager instance;
public int gridSizeX, gridSizeY, gridSizeZ;
[Header("Blocks")]
public GameObject[] blocksList;
public Transform[,,] theGrid;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
theGrid = new Transform[gridSizeX, gridSizeY, gridSizeZ];
SpawnNewBlock();
}
// Update is called once per frame
void Update()
{
}
//Round block position
public Vector3 Round(Vector3 vec)
{
return new Vector3(Mathf.RoundToInt(vec.x),
Mathf.RoundToInt(vec.y),
Mathf.RoundToInt(vec.z));
}
//Check position of block inside of grid?
public bool CheckInsideGrid(Vector3 pos)
{
return ((int)pos.x >= 0 && (int)pos.x < 5 && (int)pos.z >= 0 && (int)pos.z < 5 && (int)pos.y >= 0);
}
public Transform GetTransformOnGridPos(Vector3 pos)
{
if (pos.y > gridSizeY - 1)
{
return null;
}
else
{
return theGrid[(int)pos.x, (int)pos.y, (int)pos.z];
}
}
public void UpdateGrid(Block block)
{
//Delete possible parrent Objects
for (int x = 0; x < 5; x++)
{
for (int z = 0; z < 5; z++)
{
for (int y = 0; y < gridSizeY; y++)
{
if (theGrid[x, y, z] != null)
{
if (theGrid[x, y, z].parent == block.transform)
{
theGrid[x, y, z] = null;
}
}
}
}
}
//Fill in all child Objects
foreach (Transform child in block.transform)
{
Vector3 pos = Round(child.position);
if (pos.y < gridSizeY)
{
theGrid[(int)pos.x, (int)pos.y, (int)pos.z] = child;
}
}
}
//Spawn New Block
public void SpawnNewBlock()
{
//Block Spawn Pos
Vector3 spawnPoint = new Vector3(0, (int)transform.position.y + gridSizeY, 0);
int randomIndex = Random.Range(0, blocksList.Length);
//Spawn the block
GameObject newBlock = Instantiate(blocksList[randomIndex], spawnPoint, Quaternion.identity) as GameObject;
//Set Input
}
}
And Block Script:
public class Block : MonoBehaviour { public static Block instance;
float prevTime;
float fallTime = .2f;
private void Awake()
{
if (instance = null)
{
instance = this;
}
}
void Start()
{
GameController.instance.SetActiveBlock(gameObject, this);
}
void Update()
{
if (Time.time - prevTime > fallTime)
{
transform.position += Vector3.down;
if (!CheckValidMove())
{
transform.position += Vector3.up;
enabled = false;
//Create a new tetris block
GameManager.instance.SpawnNewBlock();
}
else
{
//Update the grid
GameManager.instance.UpdateGrid(this);
}
prevTime = Time.time;
}
}
public bool CheckValidMove()
{
foreach (Transform child in transform)
{
Vector3 pos = GameManager.instance.Round(child.position);
if (!GameManager.instance.CheckInsideGrid(pos))
{
return false;
}
}
foreach (Transform child in transform)
{
Vector3 pos = GameManager.instance.Round(child.position);
Transform t = GameManager.instance.GetTransformOnGridPos(pos);
if (t != null && t.parent != transform)
{
return false;
}
}
return true;
}
public void SetRotationInput(Vector3 rotation)
{
transform.Rotate(rotation, Space.World);
if (!CheckValidMove())
{
transform.Rotate(-rotation, Space.World);
}
else
{
GameManager.instance.UpdateGrid(this);
}
}
}
Anyone have ideas to fix this for me please! Thank you very much!
Your answer
Follow this Question
Related Questions
Stereo Sounds In 3D Space 1 Answer
How can I stop the editor UI from flickering? 0 Answers
How to implement the ability to move and place objects in HoloLens Application? 0 Answers
I get errors that say GameCoreXbox after installing Cinemachine 0 Answers
Unity Gameobjects aren't showing up in the hierarchy but can see them in gameview 0 Answers