Card games are amongst the oldest types of games and have been popular for centuries. In this article we will examine how to make a card game in Unity. Making card games is not only fun, but also a great way to build game development skills.
Discover how to make a card game in Unity
Today we will create a simple card-based Memory game using items from the Asset Store combined with custom written components.
PART 1 - Project and Asset Setup
1. Create a New Project.
Open Unity Hub and create a new 3D Project called “Memory”.
2. Get Playing Cards Graphics Asset Package.
One of the amazing things about Unity is the Asset Store. The Asset Store is a deep resource which can supply game developers with ready-to-use assets at low or no cost. In our case, our card game will require textures and models with which to display playing cards.
- Go to the Asset Store in a web browser here.
- Search for “Free Playing Cards - Ultimate Sport Pack”.
- Click the “Add to My Assets” button.
3. Import Play Cards Graphics Package to your project.
- Back in Unity, open the Package Manager from the “Windows” menu.
- Choose “Packages: My Assets” from the package selector.
- Download and Import the “Free Playing Cards - Ultimate Sport Pack” assets.
- Create a “Resources” folder inside of “Assets” and move “Free_Playing_Cards” within it. “Resources” is recognized by Unity as a special folder which allows contained assets to be loaded directly from code using the Resources.Load() method.
4. Now that we have our Playing card assets, we can start laying out our game board.
- Right-click in the Hierarchy panel and create a Square Sprite. Name it “GameBoard”
- Choose a Color for your GameBoard and Resize it to match the gray Viewport frame.
- In the Inspector Panel, choose “Add Component” -> “New Script” and name it “MemoryGame”.
5. Now it’s time to set up a Playing Card!
- From the Project Panel, in “Assets/Resources/Free_Playing_Cards” drag the card PlayingCards_2Club onto your GameBoard item in the Hierarchy View. At first, you will not see it. This is because you must set the rotation, scale, and position so that it is visible.
- We will make this card into a Prefab and reuse it for all of our game cards.
- Rename the card in the Hierarchy window to “PlayingCard”
- In the Inspector Panel, choose “Add Component” -> “New Script” and name the script “Card”.
- Drag the PlayingCard from the Hierarchy panel to the Assets folder to make it into a Prefab.
- When Unity asks you how to create the Prefab, choose “Original Prefab”
6. Make “PlayingCard” instances.
Create as many “PlayingCard” instances on the GameBoard as you wish, in whatever layout you wish. Keep in mind that you should end with an even number so that all of the cards can be matched. Make sure the cards are children of the GameBoard.
7. Brighten the cards up.
The playing cards look pretty dark. Let’s create a GameObject -> Light -> Directional Light to brighten things up, and set its intensity to 2.
PART 2 - Scripting the Game
Open up the MemoryGame.cs script
Set up some variables that for general use.
And now some variables for configuration and game state.
Implement our Shuffle and GetRandomFromArray functions.
Finally, implement our game logic. Whenever a card is selected, store it to be compared.
When we have two cards selected, wait one second and then see if they are a match. If they are a match, remove them from the board. If not, flip them back over.
We’re almost done! We just need to implement our Card functionality in Card.cs.
Establish state variables.
Set and check rank and suit.
Respond to user actions.
And finally, detect and respond to mouse clicks!
You should now have a fully functional card-matching game!
Now you know how to make a card game in Unity
Today we used freely-available Asset Store card assets with two custom-coded components to build a card-matching game. The same code can be modified to make a variety of card games… Blackjack, Poker, the possibilities are endless!
Up next, learn how to make a cool game in Unity.
To get a stronger and deeper understanding of the Unity development system, sign up for Create & Learn’s Unity development classes. There are two units of lessons to help you get comfortable and get running with Unity game development!
Written by Ken Patel, who started coding at age 10. Following his passion for video games, when he entered university he enrolled to study Computer Science. After graduation, he embarked on a career as a professional video game developer. Ken is excited to be sharing his passion, knowledge, and experience with the next generation of game makers through his role as a teacher here at Create & Learn.