#How to make a game like Tetris in Unity ...
1 messages · Page 1 of 1 (latest)
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
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?
@flint rock OK, i slammed some neurons together and I'm pretty sure i got the solution.
Nice, thanks
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
Sorry for the late response, I have just seen the message, let me do it
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
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.
The zip is too big to upload in Discord, so here is the link to Mega to download it https://mega.nz/file/Dt8QjYzZ#cbeq-8JdN3Ch6v6FvLFvhWQfC8GSA9P9OlzcYiGVmRs
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?
Yeah, I was doing homework, thanks for pinging
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
you figured out why it gets stuck inside the wall?
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
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
Yeah, that's about it
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.
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
Sure
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;
So I take what is on Update() to Start()?
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
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
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
@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>();
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.
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
sry, didn't quite understand the last sentence of yours.
You want to understand why i suggested "currentRotation;" ?
No, I get why you suggest currentRotation, I mean I don't get this the part of "and this needs to replace the isValid-check:"
Since on this code there's no mention of esPosicionValida
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?
that one?
No, I haven't changed the rotate function at all
So it ends up like this? if(esPosicionValida()){ juego.actualizarGrid(this); //This is cause I have done what you recomended to only call the juego script once }else{ transform.SetPositionAndRotation(transform.position, Quaternion.Euler(0,0, currentRotation)); }
looks good!
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)
you sure you saved the script and restarted your game? The rotation 270 shouldn't be possible anymore with the code i provided
I will restart the program and check again