Join Kelley Hecker for an in-depth discussion in this video Make a game piece move, part of Building a Match 3 Game with Unity.
- [Voiceover] Now that we've got our basic game piece script, let's add some more features to it. We'll start by making our pieces movable. In the scripts folder, right click and create a new c sharp script and call it movable piece. This movable piece script is a component that will allow us to add mobility to any type of piece. Click on the prefabs folder and select the normal piece prefab. Go back to the scripts folder and drag the movable piece component onto the prefab. Open the movable piece script.
The movable piece component needs to know about the game piece that it's attached to, so we'll add a reference to the game piece script. In awake, we'll get the reference to the game piece. I'm using awake to make sure that we get this reference as soon as possible so that we don't have any null reference exceptions. Then we just need to add a move function which moves the piece. For now, we'll just instantly move the piece to its new position. Later, when we get to filling the board, we'll animate the pieces.
To move the piece, we need to update the x and y coordinates in the grid as well as the position of the game object in world space. Updating the coordinates on the grid is easy. They're the x and y properties from the game piece script. The grid has a get world position function that we wrote earlier, and we can use that to get the position for the game object. We're setting the position locally because the piece is a child of the grid. Since we originally wrote get world position just for the grid class, it's private.
If we want to access it in this movable piece script, we have to make it public. Looking at this, you might notice an issue. Remember that we didn't define a setter for the game piece's position, so we'll need to go back and add that, but we only want to set the x and y if the piece is movable. If it's not movable, other classes should not be able to update the position. We can check if a piece is movable by seeing if it has the movable piece component. First, we'll get a reference to the movable piece script.
Here, I've just created a private variable movable component and a public property capital movable component. Then in awake, we'll get the reference to the movable component if the game object has one. If the game object doesn't have a movable piece script on it this will just return null. So to check if a piece is movable, we just need to see if the movable component variable is null or not. We'll create a function is movable to do that. If the movable component is not null, meaning it exists, this will return true, meaning the piece is movable, otherwise it will return false.
Now back to the setter for our position. We only want to set the value if the piece is movable. So we'll use our is movable function to check that. And we'll do the same thing for the y property. I'm just going to copy and paste. And I just update the variable x to y. To check if this all works, let's go back to the grid class and change all the pieces to start at 00. To do that, we just replace get world position with vector three dot zero.
Go back to the editor and hit play. You can see that all the pieces are stacked on top of each other in the center. Now let's use our move function to move the piece to the correct spot. We first check if the piece is movable and if it is, we use the movable component to move it. Hit play, and you can see that all the pieces are now in the correct place, so our movement code is working. While our game board might not look much different, this movement code will be the basis for our animation once we start filling the board.
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.