Join Kelley Hecker for an in-depth discussion in this video Add pieces to the grid, part of Building a Match 3 Game with Unity.
- [Voiceover] Now that we have our background tiles, let's add some game pieces. Go ahead and open up the grid script. We need an array to hold all of our pieces. Since the grid has two dimensions, we'll create a 2D array of game objects. You can create a 2D array by using the comma notation. In start, we'll instantiate all of our game pieces in the grid. Just like we did with the background pieces. First I'll instantiate my game object array, to have dimensions the same as my grid.
The reason we're storing an array of the game pieces, when we didn't store an array of the background pieces, is because we'll later want to manipulate the game pieces. The background pieces, once we've created them, can just stay where they are. Again I'm going to loop through all the rows in our grid and then I'm going to loop through all the columns. I'm then going to instantiate a new game object and assign it to our pieces array. I get a reference to the prefab for the normal piece type by using our dictionary. And I'll instantiate the game object at the X and Y coordinates of the grid.
I'm then going to change the name of the game object, just so it's easier to tell which piece is which. And finally, I'm going to make the piece a child of the grid. Go ahead and hit start and we can see that a piece is created on top of each background. However, they're off center. We'll need to offset the position of the pieces to center the grid. To do that, we're going to create a function called GetWorldPosition. That will convert a grid coordinate to a world position.
What I'm doing here is getting the X position of the grid, which is the center of the grid, and subtracting half of the width to get the left edge of the grid. I'm then adding our X coordinate. Since our world units are the same spacing as our grid units, this will give us the world position for our game piece. I do the same thing for the Y coordinate, except I invert the pluses and minuses, so that our grid starts at the top. Now I'll just update our calls to instantiate for the game pieces and the background pieces to use our new GetWorldPosition function.
Now if you hit play, the game pieces are nicely centered on the screen. If I move the grid object around, all the pieces move with it. Now that we've got our grid set up, let's add some more functionality to our game piece.
Game developer Kelley Hecker covers topics like creating a grid-based game board, using inheritance to create different types of game pieces, adding obstacles and new levels, detecting matches and clearing pieces, and implementing a user interface complete with a score screen. By the end of the course, members will have a completed game and learned new techniques to apply to their next Unity project.
- Creating a game board grid
- Creating and scripting game pieces
- Filling the board
- Creating obstacles
- Swapping pieces
- Matching pieces
- Clearing obstacles and pieces
- Creating new levels
- Creating the user interface: HUD, game over screen, etc.