Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need help with my 3D tetris game.

Discussion in 'Scripting' started by chukwuboy88, May 21, 2022.

  1. chukwuboy88

    chukwuboy88

    Joined:
    Apr 22, 2022
    Posts:
    10
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TetrisBlock : MonoBehaviour
    6. {
    7.     GameLogic gameLogic;
    8.     bool movable = true;
    9.     float timer = 0f;
    10.     public GameObject rig;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         gameLogic = FindObjectOfType<GameLogic>();
    16.     }
    17.  
    18.     void RegisterBlock()
    19.     {
    20.         foreach(Transform subBlock in rig.transform)
    21.         {
    22.             gameLogic.grid[Mathf.FloorToInt(subBlock.position.x),
    23.                 Mathf.FloorToInt(subBlock.position.y),
    24.                 Mathf.FloorToInt(subBlock.position.z)] = subBlock;
    25.         }
    26.     }
    27.  
    28.     bool CheckValid()
    29.     {
    30.         foreach(Transform subBlock in rig.transform)
    31.         {
    32.             if(subBlock.transform.position.z >= GameLogic.width ||
    33.                 subBlock.transform.position.x >= GameLogic.length ||
    34.                 subBlock.transform.position.x < 0 ||
    35.                 subBlock.transform.position.y < 0 ||
    36.                 subBlock.transform.position.z < 0 )
    37.             {
    38.                 return false;
    39.             }
    40.             if (subBlock.position.y < GameLogic.height && gameLogic.grid
    41.                 [Mathf.FloorToInt(subBlock.position.x),
    42.                 Mathf.FloorToInt(subBlock.position.y),
    43.                 Mathf.FloorToInt(subBlock.position.z)] != null)
    44.             {
    45.                 return false;
    46.             }
    47.         }
    48.         return true;
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.         if (movable)
    55.         {
    56.             //Updating the timer
    57.             timer += 1 * Time.deltaTime;
    58.             //Drop
    59.             if (Input.GetKey(KeyCode.Space) && timer > GameLogic.quickFallTime)
    60.             {
    61.                 gameObject.transform.position -= new Vector3(0, 1, 0);
    62.                 timer = 0;
    63.                 if (!CheckValid())
    64.                 {
    65.                     movable = false;
    66.                     gameObject.transform.position += new Vector3(0, 1, 0);
    67.                     RegisterBlock();
    68.                     gameLogic.SpawnBlock();
    69.                 }
    70.             }
    71.             else if (timer > GameLogic.fallTime)
    72.             {
    73.                 gameObject.transform.position -= new Vector3(0, 1, 0);
    74.                 timer = 0;
    75.                 if (!CheckValid())
    76.                 {
    77.                     movable = false;
    78.                     gameObject.transform.position += new Vector3(0, 1, 0);
    79.                     RegisterBlock();
    80.                     gameLogic.SpawnBlock();
    81.                 }
    82.             }
    83.  
    84.             //Sideways movement
    85.             if (Input.GetKeyDown(KeyCode.A))
    86.             {
    87.                 gameObject.transform.position += new Vector3(-1, 0, 0);
    88.                 if (!CheckValid())
    89.                 {
    90.                     gameObject.transform.position -= new Vector3(-1, 0, 0);
    91.                 }
    92.             }
    93.             else if (Input.GetKeyDown(KeyCode.D))
    94.             {
    95.                 gameObject.transform.position += new Vector3(1, 0, 0);
    96.                 if (!CheckValid())
    97.                 {
    98.                     gameObject.transform.position -= new Vector3(1, 0, 0);
    99.                 }
    100.             }
    101.             else if (Input.GetKeyDown(KeyCode.W))
    102.             {
    103.                 gameObject.transform.position += new Vector3(0, 0, 1);
    104.                 if (!CheckValid())
    105.                 {
    106.                     gameObject.transform.position -= new Vector3(0, 0, 1);
    107.                 }
    108.             }
    109.             else if (Input.GetKeyDown(KeyCode.S))
    110.             {
    111.                 gameObject.transform.position += new Vector3(0, 0, -1);
    112.                 if (!CheckValid())
    113.                 {
    114.                     gameObject.transform.position -= new Vector3(0, 0, -1);
    115.                 }
    116.             }
    117.  
    118.             //Rotation
    119.             if (Input.GetKeyDown(KeyCode.RightArrow))
    120.             {
    121.                 rig.transform.eulerAngles -= new Vector3(0, 0, 90);
    122.                 if (!CheckValid())
    123.                 {
    124.                     gameObject.transform.position += new Vector3(0, 0, 90);
    125.                 }
    126.             }
    127.             else if (Input.GetKeyDown(KeyCode.LeftArrow))
    128.             {
    129.                 rig.transform.eulerAngles -= new Vector3(0, 90, 0);
    130.                 if (!CheckValid())
    131.                 {
    132.                     gameObject.transform.position += new Vector3(0, 90, 0);
    133.                 }
    134.             }
    135.             else if (Input.GetKeyDown(KeyCode.DownArrow))
    136.             {
    137.                 rig.transform.eulerAngles -= new Vector3(90, 0, 0);
    138.                 if (!CheckValid())
    139.                 {
    140.                     gameObject.transform.position += new Vector3(90, 0, 0);
    141.                 }
    142.             }
    143.         }
    144.         return ;
    145.     }
    146. }
    147.  
    while the code for area the tetris block is about to move freely in is
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameLogic : MonoBehaviour
    6. {
    7.     public static float fallTime = 0.9f;
    8.     public static float quickFallTime = 0.01f;
    9.     public static int length = 10, width = 10, height = 20;
    10.     public GameObject[] blocks;
    11.     public Transform[,,] grid = new Transform[length, width, height];
    12.  
    13.     void Start()
    14.     {
    15.         SpawnBlock();
    16.     }
    17.  
    18.     public void SpawnBlock()
    19.     {
    20.         float guess = Random.Range(0, 1f);
    21.         guess *= blocks.Length;
    22.         Instantiate(blocks[Mathf.FloorToInt(guess)]);
    23.     }
    24. }
    25.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Welcome to Debugging 101! Here's how to get started on your journey:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. jlivingstonsg

    jlivingstonsg

    Joined:
    Aug 22, 2012
    Posts:
    6
    -----
    If anyone want to help me dev my online 3d tetris version
    then you are welcome.

    Regards from Sweden
    ---------------
     
  4. chukwuboy88

    chukwuboy88

    Joined:
    Apr 22, 2022
    Posts:
    10
    Awesome although the camera angle is tough to work with, mine will be orthorgraphic topdown , like those from this game
    https://play.google.com/store/apps/details?id=com.kiarygames.tinyroom&hl=en with you being able to change the camera with something like a swipe or button.

    any chance you can share the code for yours for me to see how you implemented your game.
     
    Last edited: May 24, 2022
  5. jlivingstonsg

    jlivingstonsg

    Joined:
    Aug 22, 2012
    Posts:
    6