- Home /
Tetris Unity game structure C#
As a budding developer, my biggest issue with Unity seems to be figuring out how to structure a game from the outset. Once the structure is in place though, I seem to do fine in getting all the systems and code to work great.
I'm struggling to figure out how to set up a game like Tetris within Unity. Using standard design, I could create the classes and the game itself without prefabs, but that seems to go against the nature of what Unity is all about. I would rather take the 'object with components' approach.
How would you set up Tetris within Unity? I'm using primitive cubes as the blocks that make up the shapes.
Answer by Maylina · Nov 19, 2017 at 01:57 PM
It is not reusable to make everything depends on Unity. Better to use 2D Arrays and draw block according to array data.
Here sources of such tetris game: http://doctrina-kharkov.blogspot.com/2017/11/kak-napisat-tetris-c-sharp-unity-1.html
Answer by Meltdown · Mar 22, 2012 at 07:35 PM
Simple. Just create a model for each shape piece, then add a mesh collider. As the pieces fall and connect with another piece, set them to isKinematic = false so they stop moving.
Answer by ptpaterson · May 31, 2012 at 12:29 AM
I am looking around for ideas relating to a tetris game, too. Probably more like the Japanese game, puyo puyo, but the details I would think are similar.
I wouldn't use the in game physics by making the blocks rigid bodies. Rather, you could create a 2d array to store all of the positions of the fallen blocks/pieces/gooballs. You could restrict the falling pieces to the grid as well, or let them move more freely and use the colliders to determine when the falling piece has landed. When a falling piece collides with a landed piece, then you could start a timer only allowing the piece to rotate or move down. Then you can store that piece in your array and calculate whether to eliminate a row or a group of pieces or whatever. You would have complete control over how the pieces move, and I think it would make for MUCH smoother gameplay.
You could still use cubes to represent the pieces as a start, or use any other mesh and use a box collider around it. But if you use the built in physics than it might end up bouncy and harder to control, like this video I just found.
I think that prefabs would be great for things like the differently shaped pieces. Though it might not be so necessary for the interface.
Your answer
Follow this Question
Related Questions
Instantantiate relative to parents position 1 Answer
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers
Camera follow script not following player? 1 Answer
How do i Instantiate a prefab with specific assests included 3 Answers
Properly reverting to a cashed state 0 Answers