How to make Tetris in Unity (Complete Tutorial) 🧩🧱
Vložit
- čas přidán 22. 07. 2024
- Learn to make the classic 2D arcade game Tetris in Unity. Tetris is a tile-matching video game created in 1984. In Tetris, players complete lines by moving differently shaped pieces, which descend onto the playing field. The completed lines disappear and grant the player points, and the player can proceed to fill the vacated spaces. The game ends when the playing field is filled. The longer the player can delay this inevitable outcome, the higher their score will be.
0:00 - Introduction
1:01 - Project Creation
1:56 - Importing Sprites
4:18 - Scene Setup
7:23 - Tilemap Setup
12:33 - Tetromino Data
26:00 - Spawning / Setting
43:02 - Movement
57:43 - Rotation
1:10:55 - Wall Kicks
1:21:58 - Steps & Locks
1:31:50 - Line Clears
1:43:35 - Ghost Piece
1:56:51 - Outro
🖥️ Source code and assets: github.com/zigurous/unity-tet...
👍 Subscribe for more Unity tutorials: czcams.com/users/zigurous?sub...
💬 Join our Discord community: / discord
👑 Become a Patreon supporter: / zigurous
❤️ Support my work: www.paypal.com/donate?hosted_...
⭐ Download my Unity assets: zigurous.com/assets
····················································································
► Learn more about my work: zigurous.com
► Follow for news and updates: / zigurous
#unity #unitytutorial #gamedev #unity3d #unity2d #gamedevelopment #indiegame #indiedev #madewithunity
Visit the GitHub page for the most up to date source code: github.com/zigurous/unity-tetris-tutorial
View the FAQ for answers to common questions: github.com/zigurous/unity-tetris-tutorial/wiki
@Tothegreat PC, but I'm sure you can tweak it so it can run on mobile, but obviously would have to code it for each respective platform (Andriod, Apple, ect)
i m having an issue at 1:21:36 where the trtris is moving correctly but when i play the game error is showing as index was outside the bounds of array!! what should i do ??
@@krupasanchaniya4191 I have a solution to this.......after much effort
replace the GetWallKickIndex() function with
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private int GetWallKickIndex(int rotationIndex, int rotationDirection) {
int wallKickIndex = rotationIndex * 2;
wallKickIndex = -8;
return Wrap(wallKickIndex, 0, data.wallKicks.GetLength(0));
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
and then it works.........though I am not sure..if this is an acceptable solution but it worls
These tutorials are so so helpful and good quality thank you! Please don't stop making them
Incredibly helpful! As someone who's approaching Unity with more of a background in coding, this was exactly the foothold I needed to get started. I appreciated how you left us with a fully functioning "game" but pointed out how there's still so much that could be added. I do feel like there was a good amount of "magic math" that I didn't really understand why we were doing. If you had given us some kind of reference to better understand that math on our own, I would have appreciated it. But as it is, this tutorial helped get me excited to learn more about Unity
One of the all-around, best unity tutorials I've seen. Thank you!
Man you can't imagine the useful and valuable that these videos are. I hope that I can pay you back in the future for all the knowledge that you are giving for free!
Nice that you show the end result at the beginning of your video. I'm missing this in so many tutorial videos.
Man, these tutorials are lit! I'm literally learning how to use Unity in no time thanks to you! If I can I'd like to suggest Donkey Kong, and maybe the first Super Mario Bros. and The Legend Of Zelda (even if these last two are maybe a little complex, you can maybe do a tutorial divided in more parts)
tutorials like this deserve more thumbs up. very well explained
Hope this channel blows up honestly. A few minutes in and I'm already loving it. Much love, I'll make sure to share the channel around
AWESOME! Best game development series i've ever seen.
This was a wonderful introduction into Unity and C#. Thank you :)
Thank you so much for the tutorial! Its really helping to create a big project
Best tutorial I've seen on using Unity. Thank you sir.
Thank you so much for the tutorial. I will add up adding some GUI and stuff, but this helped me a lot!
Thumbs up!
crazy good, elegant code, smart solutions. I love this
Incredibly good tutorial. Thank you
Thank you man, these tutorials are keeping me inspired in gamedev and its helping me get out of 'tutorial hell'
Great tutorial was more complicated then expected but u explained it well =)
Dude, this video is amazing! Thank you
Thanks for making this tutorials! Really help me a lot!
Thank you very much for sharing the very valuable contents. This will help me to understand some basics of Unity, C#, and Tetris! большое спасибо!
Thanks for your effort and sharing your knowledge! Have left a subscription.
I admit that center-focused positioning on a tile grid tends to irritate me more than a coordinate system from 0 to X (probably because the tiles / pieces follow a top-to-bottom path).
But as a beginner, there are many things that can be confusing and overwhelming at first.
Estou gostando do jeito que você ensina! Parabens!
Really educational tutorials Adam. I'll become a Patron after making money from the skills you've taught me :p
your tutorials are amazing. id love to see a turn based combat game like pokemon
Mind blowing bro; i made the game literally word by word in 10 hrs. i am newbie to unity.(just installed 10 days ago). and it was marvellous doing the same. thought i made it 5 year ago in c++ directx11 by my own with heavy glitches.(dont know wall kicks and bounds and wrap function.....). Marvelllous job man'
@Tothegreat it would only work on PC because we chose 2D core and not 2D mobile
I made a Tetris game some years ago using JavaScript/html but a number of bugs made me frustrated so I didn't finish the game.
With your tutorial I made a Tetris game in Godot 4. The concepts and the way you made the game gave me very good ideas how to do it in Godot. Thank you
Great Tutorial As Ever. The 5th one I have done of yours.Will do 'Flappy Bird' next.
THIS WAS AWESOME!!
This will be a million-view channel in the future!
Thanks for the video. You helped me. ;)
thank you for the Tutorial😇
Your channel is gonna rock the ceiling !!!!
Another well explained tutorial 👍 You probably have alot of tutorial requests by now. But The original Angry Birds would be really sweet 🐷🐤
great tutorial
First 20 minutes: "Hmm, seems like a nice tutorial. Gotta be easy probably..." 21:50 oh my god i'm dying xD
bro you are so good
VERY COOL!!!!
Please make a tutorial on bubble shooter
That would be a great one to make. I'll add it to the list!
Thank you for what you do. Your tutorials are amazing. Could you make the tutorial about chess or backgammon game?)
thanks you just earned a massive sub and like and share :)
I think it would be better if you would explained the moment with data, because it is really difficult to understand the creating blocks moment. It felt like cheating when you follow but cannot understand what happened and why.
But I want to thank you for this videos. They are great source for learning.
Big thanks from one russian student-programmer)
Аналогично 🙋♂
great! thank u
thanks!!
Me at the start: how complicated could tetris be?
Me at the end: yeah now it makes sense why I couldn't make it by myself...
Thanks for the tutorial, although I think at times it goes too fast it was very nice.
Thanks a lot
Thank You!!! This is Awesomee!!
It would be incredible if you teach us how to do flappy birdd!
thanks a lot
Hey, I've been loving this tutorial but I have a problem with the mino rotation: the minos rotate around the tilemap origin and not their own origin, so when they move off of the tilemap origin their rotation gets all wonky.
when TestWallKicks,I find that passing originalRotation instead of rotationIndex will return the right Wall Kick Index.
Great
hey i am stuck in the 17:57 i cant fine the new tetrmoino in the board script i did the exact same thing but its not there in the editor
please help
Edit: Yes i knew why becouse i wrote it with t small cap beside the array it should be written with T not t
Could you do a custom grid? I have an idea for my indi version. Thanks for posting this
Thank you so much for the tutorial. Although I would recommend to show some slides or pictures on what you are going to do before writing code Instead of writing alot of code and finally showing in the editor.
For some reason this doesn't allow for t-spins. I even doenloaded your source code to check if it was my mistake, but it doesn't look like it. Could you give me some guidance on what I could do to fix that?
Edit: the GetWallKickIndex function returned an int that was greater than the result be two (probably because we call it after rotating and not before), so I just had to put a subtraction by two before calling the Warp function, and that solved it.
This has been a great tutorial, I'm just fascinated by this absolutely bizarre I block rotation. I can see that you've set the pivot point as specified on the Tetris Wiki, I just don't really understand where they got that from. That is not how the I block rotates in classic NES Tetris, it rotates on the third block from the left. To do it the way they suggest would constantly shift it back and forth by an entire row or column as you rotate it, which is not how any Tetris I've ever played actually works. Any idea where this idea even came from and why they would suggest doing it that way?
best in town
Does anyone know was the files under the .vscode are for in this Github? The two files are launch.json and settings.json. Not totally sure what they're for.
Anyone know why when I try to do something similar 1 pixel to the right and left of the grid image is being cut off?
Is 192 pixels per unit optimal for something (resolution reasons, etc) or is it just how big the tiles were drawn as?
Flappy Bird next!
I need to do same tetris program, but i also need to show next falling piece, and i dont kow how i can do that.
Thi shas been a great tutorial, i do have a few other questions becuase ive been having trouble with it, how do i add a sound effect when the line clear activates?
You'd have to add an AudioSource component to your game object. You can assign an AudioClip asset to that component for whichever sound you want to play, then you would get a reference to the AudioSource component in your script, and finally call the function Play() on it: docs.unity3d.com/ScriptReference/AudioSource.Play.html
so i get no errors on the ghost script , but for some reason the ghost tile appers one slot outside the bottom on the baord, and if i stack them or if the pieces touch they disaperas, help
This was great. I personally made the ghost drop logic similar to the lineclear logic- we have access to Board, so we get board.bounds for free. Then just loop from bottom row up until IsValidPosition returns.
the IsValidPosition doesn't check what blocks are above it, so your suggestion wouldn't work as it would incorrectly declare a space valid, when it is blocked by tiles above.
Bro at 2:56 what you dragged to get those colors please tell
I would love if you made a dig dug remake at some point,it's a bigger project than your usual videos, but will be a great project to work on.
Thank you
Sees sin and cos, feels fight or flight response build up internally
Can you make a video about 3d tetris?
if yes, can you provide the link for me !
Thank you so much for the tutorial
Hello dear how can i add score and game over and restart option in tetris project
Can you make a tutorial on making Tetris with the Arika Rotation System?
thank you, Random yutuber +1 sub for you
Hello, i added a simple counter 100pts every line, but how do i implement a system that check if you clean 1 or 2 or 3 or 4 lines at the time and +100 / +300 / +500 / +800pts like the original game ?
in the ClearLines loop, count the times if(isLineFull) is triggered, after the loop ends, then calculate the score with the amount of cleared lines that happened
hello, please after following the tutorial, my spawnpiece are not showing on the board and its not throwing error...please i dont know if someone can help me
why have new values for when it might potentially go through a wall? why not instead just use the same values, but scoot them over programmatically?
Hello Zigurous, thank you for this awesome tutorial. How can I implement a score when lines are cleared? And show them on the interface? Maybe you explained it in another tutorial?
I'm not Zigurous, but I think you still can apply a score, but it will need to resize, create a new part to make the score and don't forget to function it. Again I'm not Zigurous but I need to share my opinion.
@@akmalahza4951 this game is for Android?
@41 I am getting null reference exception, I dk why?
in 42:29 on the spawn positions i type the same cordinates as you and the tetrominoes are not spawning in the top of the board
If you post the code for your SpawnPiece function, I might be able to tell what the problem is.
Hi, a very cool lesson, thank you) But I have a problem with turning when the shape is next to the shape. That is, she turns despite the fact that there is another figure standing there
How do you put the shapes into unity??
1:27:27 It's a bad idea to check lockTime in the Step function. If stepDelay = 2 and lockDelay = 0.5 we have lockTime always 2;
stepDelay = 0.3, lockDelay = 0.2, then lockTime always = 0.3;
stepDelay = 0.2, lockDelay = 0.3, then lockTime always = 0.4, and so on
hello Adam thx to much, for te tutorial, but i would like play this game on my cellphone, how can I chance the comands
If i try to test the lineclear, unity crashes always? What should I do
Hey man. How did you learn to make games? I've been trying to code games for 4 years but I can't seem to just be good and confident at writing C# scripts and separating them efficiently. Even this simple game is confusing me. :(. Did you have a tough time as well when you started?
It's definitely tough and takes a lot of practice. Like most things, if you keep doing it more and more you will get better and better, just takes time. I was lucky that my high school offered a programming class that introduced me to writing code.
Keep watching tutorials, perhaps tutorials that are strictly programming not necessarily game dev. Brackey's has a playlist for beginners czcams.com/play/PLPV2KyIb3jR4CtEelGPsmPzlvP7ISPYzR.html
@@Zigurous I am so happy that clicked in the link😁😁, I am new to programming and I am just kid which wants to create minecraft because it costs money😄😄
Where did you design the sprites?
Can I ask why I can not call Move() function outside the script Piece.cs?
move () is defined within the pieces.cs script so in order to call it from other scripts you'll need to create an object of the class and call pieces.move()
Why dont u put Start Button or Menu, End Game at the end Score?
unity read key is unsable,
sometimes i pressed, there is no reaction
sometimes i pressed once, the block moves 2 step
is that only my computer problem?
thx
Can anyone please explain to me how to make that WallKicksI array? Thank you you guys!!!
The wallkicks array is essentially the SRS rotation which tells a piece where it can move. The parameters for the wall kicks array are the changes in vertical position and change in horizonal position. There are 4 checks that need to be made so there are 4 arrays per pieces.
How can I add shapes to the game?
For some reason at 55:44 my piece moves over the boarder by one then freezes, i tried just lowering the tile map and that fixed the going over the boarder problem but it still freezes when i go to the far right or left
It seems like you have an off by one problem. Make sure you have set your board size to match up with your tilemap. In the video we use a 10x20 board. I recommend even numbers too. Your board and tilemap should be position at the origin 0,0,0.
There could be a problem with your code, but it's hard to say specifically what. In a sense, the code seems to be working but the bounds are not correct. In the Board script in the function IsValidPosition we use the bounds to check if the board contains a tile. The bounds is calculated from your board size. This would be the first place I would look for problems.
Hey I don't know whether this is helpful or not but I had the same issue and fixed it.
In Board.cs file, public bool IsValidPosition, check if you typed
"Vector3Int tilePosition = piece.cells[i] + piece.position;"
instead of
"Vector3Int tilePosition = piece.cells[i] + position;".
I hope it helps.
@@chansongpark6259 You're the best person in the entire universe. I know you sent this 3 months ago, so I'm not expecting a reply, but thank you :D
@@chansongpark6259 i m having issue at 1:21:36 when i play game and press Q error pop out as index was outside the bounds array ?? what should i do?? help me out!!
@@chansongpark6259 DUDEEEEEEEEEEEEEEE thank you so much!
hey I can't assign the Piece script in the board I need help
same
for some reason when i try to run it (im not in the movement part) it doesnt spawn the piece
At 1:01:00 why is a Wrap method necessary? I think a normal modulo would do the trick: `this.rotationIndex = (this.rotationIndex + direction) % 4`
The Wrap would make sense if your bounds were something weird like 3->6 and you needed 7 to map back to 3. In this case the bounds are simple [0,3] so Wrap seems like overkill
If I recall correctly I believe the issue is that this doesn't work for negative numbers. If the index is 0, and you rotated left (direction = -1), then (0 + -1) % 4 = -1 which is an invalid index. The wrapping function in the tutorial supports going in either direction.
İ have the error
NullReferenceException: Object reference not set to an instance of an object
Board.Spawn Piece () (at Assets/Scripts/Board.cs:41
Please can somebody help me I copy everything in this video but it don't work.😢
Please update hold piece feature please
Can you make a breakout clone? It could be a good tutorial game. (Please include how to change bounce angle of paddle if you do so)
I plan to do Breakout next
@@Zigurous i m having an issue at 1:21:36 where the trtris is moving correctly but when i play the game error is showing as index was outside the bounds of array!! what should i do ??
If I wanted to make a lemonade tycoon style game, would I just use Unity, or would I want to use something like WPF for the UI?
Most games build the UI within the engine, Unity in this case. Unity has an entire UI system for building interfaces. It might not be the best system if you are an experience UI developer, but it would probably be harder to build and integrate it outside the engine.
@@Zigurous Great. Thanks for the reply.