#How to make a game like Tetris in Unity ...

1 messages · Page 1 of 1 (latest)

junior cape
#

@flint rock Gonna have a deeper look in an Hour, have to Drive home first

flint rock
#

Okey, many thanks, I know the code in the video has the same problem cause some people in the comments say that they have it too

junior cape
#

OK, I'm back. While driving home I was thinking about this.

#

Did the Tutorial already go over how to disable right/left movement when the blocks reached the bottom? If so, where in the Code is that?

junior cape
#

@flint rock OK, i slammed some neurons together and I'm pretty sure i got the solution.

junior cape
#

can you show me an image of the case where the s/Z piece is stuck in the air? pls switch the Inspector from normal to debug and show me the Tetramino script of that gameobject

flint rock
#

Here it is, I will probably going to eat dinner and go to sleep after since is 9.20 PM here in Spain, so sorry if I don't answer too much

junior cape
#

What's your Grid position? Can you show the Grid Gameobject in the Scene-view? The block on the bottom left corner should be at -1,-1, right?
Your Z-Tetramino is rotated to 270, which is weird, it should toggle between 0 and 90.

#

easiest way to troubleshoo is propably zipping the project and dming it to me.

junior cape
#

wow, it's wierder than i thought, the s/zpieces move to the right, even when pressing up for some reason

#

@flint rock u readin?

flint rock
#

According to my teacher a possible solution to this problem would be for those two pieces to detect how far they are from the border and if is 0, move them 1 to the left, and the rotate

junior cape
#

you figured out why it gets stuck inside the wall?

flint rock
#

I think so

#

The (0,0) position is 1 spaces from the right border, so when it rotates the (0,0) is touching the border

#

When that happens 2 pieces of the big tetramino get into the border, and since the pieces are meant to stop if they touch the border, the piece gets stuck

junior cape
#

ok, so the Problem is that the S/Z-Pieces toggle between 0 and 90

#

the If-statements in the "Input.GetKeyDown(KeyCode.UpArrow)" part are build in a way that you do the rotation

#

then you check if it's valid and if it is not valid you rotate by -90

#

The Issue is that the S/Z-piece when in an upward position tries to go from 90 to 0 to get back to an S/z position

#

but when pushed all the way to the right you clip the wall, then instead of going back to 90, your code tells the piece to go -90

#

pushing it into the wall

flint rock
#

Yeah, that's about it

junior cape
#

now I havent quite figured out how to tackle this issue

#

a dirty way would be to rebuild the rather complicated if-else structure at the top, basically like this:

if(esPosicionValida()){
  FindObjectOfType<Juego>().actualizarGrid(this);
}
else
{
  //transform.Rotate(0, 0, -90);
  if (limitarRotacion)
{
if (transform.rotation.eulerAngles.z <= 90)
{
  transform.Rotate(0, 0, 90);
}
else
{
  transform.Rotate(0, 0, -90);
}
}
else
{
  transform.Rotate(0, 0, -90);
}
}
#

a more elegant way would be to read the orientation before the rotation, then if the validation fails return to the old orientation, but I haven't quite figured that out yet without causing bugs with the other pieces.

flint rock
#

Well, I will try this tomorrow (other homework is using my time), and I will try to see if I can make the elegant solution somehow, thanks for helping, and hope you don't mind helping me some more in the future

junior cape
#

Sure

junior cape
#

some small things:
For a small game like yours it's a non-issue, but you generally want to avoid using FindObjectOfType<>() in the Update-Method.
It's better for performance to do this stuff in Start. It also safes you the effort of writing it a dozen times.

The "new Vector3"s I'm also not too fond of. I'm not 1000% sure, but I think with the "new Vector3" you create a new variable in RAM everytime. Better use the predefined Vector3."direction". It's also more readable!
So those two lines do the same:

transform.position += new Vector3(1,0,0);
transform.position += Vector3.right;

flint rock
junior cape
#

so i fixed the isValid check, see here:
This needs to be before start:

float currentRotation;

This needs to be after "if(permitirRotacion){"

currentRotation = transform.rotation.eulerAngles.z;

and this needs to replace the isValid-check:

if(esPosicionValida()){
  FindObjectOfType<Juego>().actualizarGrid(this);
}
else
{
  transform.SetPositionAndRotation(transform.position, Quaternion.Euler(0,0, currentRotation));
                    
}
#

@flint rock currently raiding in WoW, i'll write you the other stuff later

junior cape
#

Another Problem is now: since the Rotation of S/Z goes from 0-90 the rotation correctly works on the left border, but not on the right

junior cape
#

Easiest would propably be the solution your teacher mentioned, but you need to make sure that you don't push the piece into already placed pieces

junior cape
#

@flint rock Regarding FindObjectOfType<Juego>():

First, replace all "FindObjectOfType<Juego>()" inside your Tetramino.cs with "juego"
There are several ways to do this, this is one:

Place this above Start():

Juego juego;

Then place this inside Start():

juego = FindObjectOfType<Juego>();

This requires that you only ever use juego once! (It's currently on your Grid Gameobject)

#

This way you only search for the juego script when the Gameobject this script is attached to is created.
Before, you searched for it everytime you referenced FindObjectOfType<Juego>();

junior cape
#

Found another bug:
If there are blocks placed higher than 10 on the Y, your blocks dont get pushed down after you fill up and delete rows below it.
You need to replace "anchoGrid" with "altoGrid" in Juego.cs:80

for(int i = y; i < altoGrid; ++i){

Another suggestion would be to periodically check wether your Pieces-Gameobjects don't contain any more child-gameobjects to clean up Gameobjects where all minos got deleted from.

flint rock
#

I have added all the changes you have gave me except the one of the rotate function cause I'm trying to understand how to make it up, cause you have gave me one but then you mention things that are on my function but not in the one you have gave me

junior cape
#

sry, didn't quite understand the last sentence of yours.

#

You want to understand why i suggested "currentRotation;" ?

flint rock
flint rock
junior cape
#

there yould be ```CS
if(esPosicionValida()){
FindObjectOfType<Juego>().actualizarGrid(this);
}

in your code, after that you rotate -90. The -90 line has to be replaced with:
```CS
transform.SetPositionAndRotation(transform.position, Quaternion.Euler(0,0, currentRotation));
#

unless you already copied my earlier "quick-and-dirty" version?

flint rock
flint rock
junior cape
#

looks good!

flint rock
#

Okey, I'm going to check if it works

#

The s and z still get stuck on the right border (I think you mentioned something on this one, but not sure)

junior cape
#

you sure you saved the script and restarted your game? The rotation 270 shouldn't be possible anymore with the code i provided

flint rock
#

I will restart the program and check again