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.

4.3 (6) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 3 of 5 of this article. Click here to view the first page.

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.

Note: The gravity in your starter project was set to -1 to make it easy to fly around and test the game right away.

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:

Things are about to get heavy!

Adjusting Gravity in the Physics settings

Things are about to get heavy!

Click Play to run the game; fly around a bit and see how the gravity changes the motion of your ship:

One small step up for gravity, one giant leap required for our thruster power!

One small step up of gravity, one giant leap required for our thruster power!

One small step up for gravity, one giant leap required for our thruster power!

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.

Rigidbody and Collider settings

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:

Edit Collider Button

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:

Editing a Polygon Collider 2D

Leave the shape of the Lander collider as is for now.

Note: The code in the Lander.cs script attached to the Lander GameObject uses 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:

Lander-box-collider-2D

Unity adds a Box Collider 2D component to the LanderObjective GameObject and automatically sizes the collider to match the sprite size.

Lander-box-collider-on-landing-platform

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.
Note: You may not notice it when there are only a few objects in a game, but when you have hundreds of objects onscreen, all involved in physics interactions, using simpler collider shapes will improve the performance of your game.

You may want to re-think your strategy of using Polygon Collider 2D components if you have lots of objects colliding!

You may want to re-think your strategy of using Polygon Collider 2D components if you have lots of objects colliding!

You may want to re-think your strategy of using Polygon Collider 2D components if you have lots of objects colliding!

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:

Lander-animator-component-no-controller-reference

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 WindowAnimationAnimation:

Animation Window

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:

Create an animation clip

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:

Lander animation assets created

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:

animation-add-property-unity2d

Lander-sprite-property-to-animate

Your editor should now look like this:

Time to work on the timeline

Lander-SpriteAnimation-Timeline

Time to work on the timeline