- Home /
I need help with registering blocks in my 3d tetris game.,trying to make a 3d tetris and need help registering subBlocks?
I need help in trying to register subblocks of the tetraminoes in the tetris game I am making. It is only registering one middle block where the pivot is rather than all four blocks of the tetraminoe , I have tried changing the pivot from centre to pivot and nothing is working , i think it has to do with the code but I am still new to programming and would appreciate any help. below is the code for the individual tetraminoe
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TetrisBlock : MonoBehaviour { GameLogic gameLogic; bool movable = true; float timer = 0f; public GameObject rig;
// Start is called before the first frame update
void Start()
{
gameLogic = FindObjectOfType<GameLogic>();
}
void RegisterBlock()
{
foreach(Transform subBlock in rig.transform)
{
gameLogic.grid[Mathf.FloorToInt(subBlock.position.x),
Mathf.FloorToInt(subBlock.position.y),
Mathf.FloorToInt(subBlock.position.z)] = subBlock;
}
}
bool CheckValid()
{
foreach(Transform subBlock in rig.transform)
{
if(subBlock.transform.position.z >= GameLogic.width ||
subBlock.transform.position.x >= GameLogic.length ||
subBlock.transform.position.x < 0 ||
subBlock.transform.position.y < 0 ||
subBlock.transform.position.z < 0 )
{
return false;
}
if (subBlock.position.y < GameLogic.height && gameLogic.grid
[Mathf.FloorToInt(subBlock.position.x),
Mathf.FloorToInt(subBlock.position.y),
Mathf.FloorToInt(subBlock.position.z)] != null)
{
return false;
}
}
return true;
}
// Update is called once per frame
void Update()
{
if (movable)
{
//Updating the timer
timer += 1 * Time.deltaTime;
//Drop
if (Input.GetKey(KeyCode.Space) && timer > GameLogic.quickFallTime)
{
gameObject.transform.position -= new Vector3(0, 1, 0);
timer = 0;
if (!CheckValid())
{
movable = false;
gameObject.transform.position += new Vector3(0, 1, 0);
RegisterBlock();
gameLogic.SpawnBlock();
}
}
else if (timer > GameLogic.fallTime)
{
gameObject.transform.position -= new Vector3(0, 1, 0);
timer = 0;
if (!CheckValid())
{
movable = false;
gameObject.transform.position += new Vector3(0, 1, 0);
RegisterBlock();
gameLogic.SpawnBlock();
}
}
//Sideways movement
if (Input.GetKeyDown(KeyCode.A))
{
gameObject.transform.position += new Vector3(-1, 0, 0);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(-1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
gameObject.transform.position += new Vector3(1, 0, 0);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.W))
{
gameObject.transform.position += new Vector3(0, 0, 1);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(0, 0, 1);
}
}
else if (Input.GetKeyDown(KeyCode.S))
{
gameObject.transform.position += new Vector3(0, 0, -1);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(0, 0, -1);
}
}
//Rotation
if (Input.GetKeyDown(KeyCode.RightArrow))
{
rig.transform.eulerAngles -= new Vector3(0, 0, 90);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(0, 0, 90);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
rig.transform.eulerAngles -= new Vector3(0, 90, 0);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(0, 90, 0);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
rig.transform.eulerAngles -= new Vector3(90, 0, 0);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(90, 0, 0);
}
}
}
return ;
}
}
below is the code for the area the tetraminoes are allowed to move freely in.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameLogic : MonoBehaviour { public static float fallTime = 0.9f; public static float quickFallTime = 0.01f; public static int length = 10, width = 10, height = 20; public GameObject[] blocks; public Transform[,,] grid = new Transform[length, width, height];
void Start()
{
SpawnBlock();
}
public void SpawnBlock()
{
float guess = Random.Range(0, 1f);
guess *= blocks.Length;
Instantiate(blocks[Mathf.FloorToInt(guess)]);
}
} ,Now i am trying to make a 3d tetris game, and ran into a problem of registering subBlocks within the tetraminoes, because it only registers one rather all four within the tetraminoe bringing annoying clipping issues. i would appreciate any help . here's the code for each block.
using UnityEngine;
public class TetrisBlock : MonoBehaviour { GameLogic gameLogic; bool movable = true; float timer = 0f; public GameObject rig;
// Start is called before the first frame update
void Start()
{
gameLogic = FindObjectOfType<GameLogic>();
}
void RegisterBlock()
{
foreach(Transform subBlock in rig.transform)
{
gameLogic.grid[Mathf.FloorToInt(subBlock.position.x),
Mathf.FloorToInt(subBlock.position.y),
Mathf.FloorToInt(subBlock.position.z)] = subBlock;
}
}
bool CheckValid()
{
foreach(Transform subBlock in rig.transform)
{
if(subBlock.transform.position.z >= GameLogic.width ||
subBlock.transform.position.x >= GameLogic.length ||
subBlock.transform.position.x < 0 ||
subBlock.transform.position.y < 0 ||
subBlock.transform.position.z < 0 )
{
return false;
}
if (subBlock.position.y < GameLogic.height && gameLogic.grid
[Mathf.FloorToInt(subBlock.position.x),
Mathf.FloorToInt(subBlock.position.y),
Mathf.FloorToInt(subBlock.position.z)] != null)
{
return false;
}
}
return true;
}
// Update is called once per frame
void Update()
{
if (movable)
{
//Updating the timer
timer += 1 * Time.deltaTime;
//Drop
if (Input.GetKey(KeyCode.Space) && timer > GameLogic.quickFallTime)
{
gameObject.transform.position -= new Vector3(0, 1, 0);
timer = 0;
if (!CheckValid())
{
movable = false;
gameObject.transform.position += new Vector3(0, 1, 0);
RegisterBlock();
gameLogic.SpawnBlock();
}
}
else if (timer > GameLogic.fallTime)
{
gameObject.transform.position -= new Vector3(0, 1, 0);
timer = 0;
if (!CheckValid())
{
movable = false;
gameObject.transform.position += new Vector3(0, 1, 0);
RegisterBlock();
gameLogic.SpawnBlock();
}
}
//Sideways movement
if (Input.GetKeyDown(KeyCode.A))
{
gameObject.transform.position += new Vector3(-1, 0, 0);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(-1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
gameObject.transform.position += new Vector3(1, 0, 0);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.W))
{
gameObject.transform.position += new Vector3(0, 0, 1);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(0, 0, 1);
}
}
else if (Input.GetKeyDown(KeyCode.S))
{
gameObject.transform.position += new Vector3(0, 0, -1);
if (!CheckValid())
{
gameObject.transform.position -= new Vector3(0, 0, -1);
}
}
//Rotation
if (Input.GetKeyDown(KeyCode.RightArrow))
{
rig.transform.eulerAngles -= new Vector3(0, 0, 90);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(0, 0, 90);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
rig.transform.eulerAngles -= new Vector3(0, 90, 0);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(0, 90, 0);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
rig.transform.eulerAngles -= new Vector3(90, 0, 0);
if (!CheckValid())
{
gameObject.transform.position += new Vector3(90, 0, 0);
}
}
}
return ;
}
}
and this is the code for the area in which the blocks are allowed to move.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameLogic : MonoBehaviour { public static float fallTime = 0.9f; public static float quickFallTime = 0.01f; public static int length = 10, width = 10, height = 20; public GameObject[] blocks; public Transform[,,] grid = new Transform[length, width, height];
void Start()
{
SpawnBlock();
}
public void SpawnBlock()
{
float guess = Random.Range(0, 1f);
guess *= blocks.Length;
Instantiate(blocks[Mathf.FloorToInt(guess)]);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613060819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How would I go about create multiple cursors controlled by multiple players 1 Answer
Distribute terrain in zones 3 Answers
Making Everything Public 0 Answers