In the previous post we made our Tetris clone pieces fall on their own. Now, we want to allow our player to tell the pieces when they are going to move left and right. In addition, they should also not be able to move the pieces left or right, outside of the play space. How can we move game pieces via player input?
The first thing you should do is familiarize yourself with the commands that will be necessary to actually register player input. There are three similar commands to accomplish this. These are:
How are these different? Input.GetKeyDown() will recognize the first time a key is pressed, and then you will have to press the key again each time you want to call it. Input.GetKey() will allow you to hold a key down and have it register multiple times. Input.GetKeyUp will, as you may have guessed, recognize when a key is released.
We will be using Input.GetKeyDown().
Within the Update() method, you will need to write an “if” statement to register key input. One if statement will be used for moving the piece left, and the other for moving it right.
Within the “if” statement, you can use the double vertical lines to signify “or.”
Restricting to the Play Space
While the above code will make your pieces move left and right, they will be able to continuously do so, into infinity. Obviously, we don’t want them to move more than 10 blocks to the right.
Also, consider that different pieces are going to have different widths. Each piece can be anywhere from one to four blocks wide, which means that it would be helpful to always know where their left and right sides are. That way, we don’t move the pieces outside of the play size, either by their left or right.
Fortunately, there is a relatively simple way to do this.
The first step is to go to your game piece and add a new component, one called a Box Collider 2D.
Once this has been added, we can access it to do some calculations, which will help us find the edge of the piece. The Box Collider 2D contains information on the center of the object it’s attached to, as well as it’s size. We can access this information by creating a new float variable and storing the data within the float. To do this, we type:
float pieceCenter = GetComponent ().bounds.center.x;
float pieceHalfSize = GetComponent ().size.x / 2;
pieceCenter now knows the center position of the game piece. pieceHalfSize is half the width of the game piece. Now, to find out the left and right edges, all we have to do is add or subtract the two, and store the results in new float variables.
float pieceRightX = pieceCenter + pieceHalfSize;
float pieceLeftX = pieceCenter – pieceHalfSize;
Of course, since we’ve been dealing in integers this whole time, it would be a lot more helpful if we guaranteed our game piece’s left and right sides rounded down to the nearest integer. We can ensure this happens by instead, typing the following code:
float pieceRightX = Mathf.Floor(pieceCenter + pieceHalfSize);
float pieceLeftX = Mathf.Floor (pieceCenter – pieceHalfSize);
Now, if you put your pieceRightX and pieceLeftX variables into a print statement and run the game, you should see that you get the left and right edges, in nice, clear whole numbers.
Finally, we can contain our Input if statements into one more if statement, ensuring they do not move outside of the game’s play space.
This is how to move game pieces via player input, and contain them to the game space at the same time.
If you have any questions, please leave a comment. Also, don’t forget to watch the video at the top of the post for extra clarification.