Making your First Game
Vérifié avec version: 2017.4
Making a game with the Playground is super easy! Let’s make a very simple action game, featuring a spaceship flying around and trying to avoid some asteroids.
Note: using the Playground requires knowing some Unity basics (interface, GameObjects, Prefabs) which are not explained here. If you are an absolute beginner, we recommend to go through the Interface & Essentials tutorials first.
1 - Creating the player
Start by dragging a spaceship image from the /Images/Spaceships folder, directly into the Hierarchy window. Because it has been imported as a Sprite, Unity will create a GameObject for you. This is going to be the player, so let’s call the object 'Ship', and tag this GameObject as ‘Player’ at the top of the Inspector window using the Tag dropdown. We will come back to tags later.
The object is now tagged as Player
Let’s make the ship move now. We need two components: Move to provide the interactivity, and a Rigidbody2D to make sure that the ship follows the laws of physics. Let’s drag a Move script from the /Scripts/Movement folder, onto the Inspector of the ship.
If dragging is a problem, you can also use the Add Component dropdown menu directly at the bottom of the Inspector, then type ‘move’. The script should appear as a first result.
The Add Component dropdown in the Inspector
You will notice that as soon as you add the Move script, a Rigidbody2D component is also added. This is because Move requires the Rigidbody2D to work.
Hint: Scene Gizmos
It might happen that the icons for the Playground script are huge, covering your graphics.
The Move icon is covering the whole ship!
If this happens, you can reduce their size by using the dropdown Gizmos in the Scene view. Drag the 3D Icons slider to the left until the icons are the right size.
The 3D Icons slider controls the size of the gizmos in the scene
2 - Tuning the physics
Now we need to tune the physics values of our ship. On the RigidBody2D component, we want to modify the Gravity to 0, so the ship doesn’t fall down. Also feel free to choose the preferred control method (arrows or WASD), and check Orient to direction if it makes sense for your graphics, then choose the orientation axis. Now press Play at the top of the editor to test the game.
You will probably notice that the ship drifts a lot, making it hard to control. Adjust the Friction on the Rigidbody2D, putting it to 10. If you play now, you will see that the drift is gone, but the object is also slower, way slower. This is because the force that was moving it is now countered by the friction.
As such, we need to adjust the Speed parameter too in the Move component. Bring it to 8, and press Play again. You will see the ship is way easier to control now. We just made our very first gameplay tweak! Congratulations: you are a game designer!
Hint: Play Mode
If you edit component values while in Play mode, you will lose your changes once the game is stopped. Remember to make changes only if you are not in Play mode! You should make changes in Play mode only if you are testing temporary values that you don’t mind losing.
3 - Adding obstacles and collisions
We have something playable, but it’s not really a game. Let’s add some obstacles that the ship has to navigate around by dragging an asteroid Sprite from the /Images folder. Like before, Unity will create a GameObject for us.
We need to add two components: Rigidbody2D and PolygonCollider2D. The PolygonCollider2D will make it so that this object is able to collide (touch) other objects. We need to add this component to the ship too.
Ship and asteroid show a green border: the collision shape
Once colliders are added, try pressing Play. You will notice the ship can now push the asteroid around. Don’t forget to tweak the Gravity of the asteroid to 0, or it will fall down! Also, feel free to tweak the asteroid’s parameter: Friction, Angular Friction and Mass, to make it behave the way you want. Let’s set the Mass to 10, so it becomes heavier and doesn’t fly away when touching the ship. An asteroid that big must be very heavy!
Let’s make this asteroid a threat now. You need to add a script to the asteroid called ModifyHealthAttribute, found under /Scripts/Attributes.
The ModifyHealthAttribute Inspector
Then, we need the ship to be able to detect this damage. To do that, there’s another script called HealthSystemAttribute (still under /Scripts/Attributes) that we need to add to the spaceship.
The HealthSystemAttribute Inspector
This not only gives the player health, but allows us to set the initial amount. Finally, make the asteroid a Prefab and duplicate it (shortcut: Ctrl+D for Windows, Command+D on Mac) to create a small asteroid field around the ship.
Before you duplicate the asteroid, you probably want to make it into a Prefab, so you can easily edit all asteroids at once at a later point.
Prefabs are a fundamental concept in Unity if you are making a big game or working in a team, but for now you can also leave them aside. You can focus on your first game, and then come back to them later when you have time.
4 - Adding the User Interface
We don’t have any feedback when the player gets hit though. So let’s add a UI to visualise the player’s health. Drag the UserInterface Prefab from the /Prefabs folder into the scene. You will automatically see a UI popping up when you look at the Game View, with Score and Health.
If you play the game now, you will see that hitting an asteroid will subtract one point of health! And if you hit too many, it’s Game Over!
Now that you have a full asteroid field, it’s time to test again if you can actually navigate around it without hitting too many. The game has to be difficult, but not too difficult! By repositioning the asteroids and testing the game, you are now basically working on the game balance and effectively doing level design.
5 - Adding a goal
So what is this game about? Let’s say we want the ship to collect some stars without crashing into the asteroids. Once all the stars are collected, the game is won. But if you crash too much, it’s game over!
Let’s add a star from the /Images folder by dragging it into the scene. Add a Collectable script from the /Scripts/Attribute folder. This will make the star a collectable item, so getting one will award one point to the player.
But how do we detect if the star has been collected? We will use collisions again. Add a PolygonCollider2D to it, but this time we want to make it a trigger so enable the Is Trigger property.
Triggers are a special type of collider. They make an object intangible, so it looks like it can’t touch other objects. But Unity will detect when the two objects touch each other, so you can perform actions as a result.
For instance, triggers are very useful to detect if a player has reached the end of the level. Place one just before the exit or end point, and when the player touches it, you display a You Win message. The player doesn’t see anything, but the trigger provides you (the developer) the logic to detect the winning condition.
6 - Winning condition
If you play the game now, you will notice the star gets collected by the ship. At this point, you might also want to make the star a Prefab (see note about Prefabs above), and then duplicate it as many times as you think it’s necessary. Let’s say we have 5 in the scene. Now we want to distribute them around so some are easy to get, while the further ones are more difficult. This way we also create a ramping difficulty for our little game.
Finally, you want to select the UI GameObject, and on the UI script make sure that the type of game is Score, and that the required score is 5. If this doesn’t match the number of stars in the game, then you might actually create a game that’s impossible to win!
Press Play again, and check that you can win the game. If you get all 5 stars, you should see a message saying "Player 1 wins!".
Congratulations: you have just made your first game. Good job! Keep tweaking it until you’re satisfied with the controls, the difficulty, and the level layout.
Now that you have an initial grasp of Unity and the Unity Playground, you can go ahead and make more complex games with it. As a first step, you probably want to take a look at the General Concepts of the Playground to understand it better.
Remember: the Playground is meant to be flexible, so come up with your own game genres or copy some existing game, anything is fine!
Note: When learning how to make games, copying the gameplay of old games from the 80s (like Asteroids, Arkanoid, Space Invaders, Frogger, and so many others…) is usually a good idea, because they were simple. Then as you improve, you can add more and more details and refine the interaction.
If you need inspiration, open the Examples folder and launch one of the games. Inspect the GameObjects and see how we have made those, then try and create something similar.