Introduction to Unity 2D
This introductory Unity tutorial will get you up and running with Unity 2D by creating a lunar landing game. By Ben MacKinnon.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Introduction to Unity 2D
35 mins
- Getting Started
- Sprites in Unity
- Sprite Modes
- Sprite Editing
- Slicing Spritesheets
- Custom Slicing
- Assigning Sprites to the Lander
- The 2D Camera and Pixels Per Unit
- A Word on Pixels Per Unit
- A Galaxy To Be Proud Of
- An Increase in Size
- Textures
- 2D Colliders and Physics
- Colliding With Objects
- A Quick Lesson on Physics Components
- Colliding Polygons
- A Few Other Things
- Lander Animation
- Unity Animation 101
- An Animator Controller for Thrusters
- Working on the Timeline
- Configuring the Animation Controller
- Sprite Sorting and Layers
- Time to Add Some Layers
- Fine Tuning Layers
- Where to Go From Here?
2D Colliders and Physics
Unity lets you adjust the gravity for the Physics 2D system just as you can in 3D games. Unity’s default gravity settings for a new project are the same as Earth’s gravity: by definition, 9.80665 m/s2. But you’re landing your spaceship on the Moon, not Earth, and the gravity on the Moon is roughly 16.6% of Earth’s, or 1.62519 m/s2.
To modify the gravity of your game, click Edit ▸ Project Settings Then select the Physics 2D tab and use the Physics 2D panel to change the Y value of Gravity from -1 to -1.62519:
Click Play to run the game; fly around a bit and see how the gravity changes the motion of your ship:
Colliding With Objects
If you’ve already tried to navigate the Lander around the scene, you’ve likely collided with a rock or two. This is Unity’s 2D collision system at work.
Every object that should interact with gravity and other physics objects requires a Collider 2D component and a Rigidbody 2D component.
Select the Lander GameObject in the Hierarchy.
You’ll see a Rigidbody 2D and Polygon Collider 2D Component are attached. Adding a Rigidbody 2D component to a sprite puts it under control of Unity’s 2D physics system.
A Quick Lesson on Physics Components
By itself, a Rigidbody 2D component means gravity will affect the GameObject it is attached to. It also allows you to control the entity from scripts using Physics2D related methods that apply forces to it.
But, if you want your sprite to interact and collide with other objects, you’ll also need a Collider 2D component. Adding an appropriate collider component makes a sprite respond to collisions with other sprites.
Polygon 2D Colliders are more performance-heavy than other simple colliders such as the Box or Circle Collider 2D components, but they make more precise physical interaction between objects possible. Always use the simplest collider shape you can get away with in your game to ensure you achieve the best possible performance.
Colliding Polygons
Explore the collider on your spaceship by selecting the Lander GameObject in the Hierarchy and clicking Edit Collider on the Polygon 2D Collider:
Hover your mouse cursor over the collider edges in your scene view. Handles appear to let you move the collider points around, and you can also create or delete points to modify the shape of the collider:
Leave the shape of the Lander collider as is for now.
OnCollisionEnter2D
to handle collisions with other objects in the game scene. If the magnitude of the collision force is above a certain threshold, the lander will be destroyed.Your landing pad also needs a collider; otherwise your spaceship would fall straight through when you tried to land!
In the Hierarchy, double-click the LanderObjective GameObject to focus on the landing pad. Using the Inspector, click Add Component and choose the Box Collider 2D component:
Unity adds a Box Collider 2D component to the LanderObjective GameObject and automatically sizes the collider to match the sprite size.
Cool!
A Few Other Things
There are a few other things to keep in mind regarding Rigidbody and 2D Collider components:
- Change Rigidbodies to use the Kinematic body type when you want to move your physics bodies via a transform component instead of only letting gravity affect them. To leave them under control of Unity’s gravity, use Dynamic. If they won’t be moving at all, set them to Static.
- You can also modify mass, linear drag, angular drag and other physics properties on your Rigidbody components.
- Colliders can be used in Trigger mode; they won’t physically collide with other physics objects. Instead, they let your code react to an event using the
OnTriggerEnter2D
method available on all MonoBehaviour scripts. - To handle collision events in your script code, use
OnCollisionEnter2D
, which is available on all MonoBehaviour scripts. - You can assign optional Physics2D Material 2D references to your Colliders to control properties such as bounciness or friction.
Lander Animation
Your lander wouldn’t be complete without visible thrusters boosting out to counter gravity. Right now, the thrusters work, but there’s no visual feedback to tell you they’re firing.
Unity Animation 101
To assign an animation to GameObjects in your scene, attach an Animator component to the GameObject(s) you wish to animate. This component requires a reference to an Animator Controller that defines which animation clips to use and how to control these clips, along with other “fancier” effects such as blending and transitioning of animations.
An Animator Controller for Thrusters
In the Hierarchy, expand the Lander GameObject to reveal four other nested GameObjects. Select the ThrusterMain GameObject; you’ll see it already has an Animator component attached, but it doesn’t reference an Animator Controller:
With the ThrusterMain GameObject still selected, click the Animation editor tab. If you don’t see this tab in the editor’s main window, open it by selecting Window ▸ Animation ▸ Animation:
When you have a GameObject selected in the Hierarchy (which you do!), the Animation window will show animations related to that GameObject – just like the Inspector shows components related to that GameObject. Click Create to create an Animation Clip for ThrusterMain:
Enter the name ThrusterAnim and place it in the Assets / Animations folder.
You should now see two new animation assets in the Animations folder of the Project window. ThrusterAnim is the Animation Clip that will hold the animation for the thruster effect, and ThrusterMain is the Animator Controller that will control the animation:
You’ll see an animation timeline in the Animation window; this is where you can place and order the individual thruster sprite frames.
Click Add Property and choose Sprite Renderer / Sprite as the type of property to animate:
Your editor should now look like this: