Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by A14Studio · Mar 12, 2020 at 11:53 PM · 3dunityeditortetris

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. alt text

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.

alt text

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!

a.jpg (30.1 kB)
b.jpg (35.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

234 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges