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 ;
}
}
while the code for area the tetris block is about to move freely in is
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)]);
}
}