What’s New in Unity 5: Unity 2D Deep Dive
Learn about the new features introduced for Unity 2D games in Unity 5, such as effectors, audio mixers, and more! By Brian Moakley.
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
What’s New in Unity 5: Unity 2D Deep Dive
40 mins
Recently at this year’s GDC, Unity Technologies dropped an exciting bomb on the audience.
Not only was the latest and greatest version of Unity 5 released, but all the professional version features were now available for free!
Unity 5 also brought forth a wealth of new features from the Physical Shaders to the Physics Engine and so on. In fact, we’ve covered the updates in our What’s New in Unity 5: Quick Summary.
Unfortunately, the only way to find out about the changes to Unity 2D was to dig through extensive release notes. To make matters worse, some of the more subtle yet impactful changes were buried deep, so if your project failed to properly migrate, you were stuck with a broken game.
In this tutorial, you’re going to learn about the new features, and you’ll see why you should be at the edge of your seat over this update. As you work through the tutorial you’ll learn about the following:
- Changes in Component Access
- Physics Effectors
- Adding Constant Force
- Audio Mixer
You’ll learn about these features by completing a game. This game isn’t about saving the world; it’s about destroying it.
Note: This tutorial assumes you are familiar with the basics of making Unity 2D games, and want to get up-to-date with the latest changes. If you are new to Unity 2D, check out Chris LaPollo’s Getting Started with Unity 2D tutorial series first.
Note: This tutorial assumes you are familiar with the basics of making Unity 2D games, and want to get up-to-date with the latest changes. If you are new to Unity 2D, check out Chris LaPollo’s Getting Started with Unity 2D tutorial series first.
Getting Started
Before you delve into the nitty and the gritty, download the starter project. Unzip it and open StarterProject\Assets\Scenes\Main.unity in Unity. You should see something that looks like the following:
The scene is set for tragic and utter destruction; the camera is already set up and a starry night background is in place. The foreground contains buildings in pristine shape, and they are composed of a few sprites taken from opengameart.org.
How tranquil.
This particular set is produced by an artist named hc. Each of the buildings consists of individual sprites placed on a sorting layer named City. Within the buildings are three smoking particle systems each so that as the buildings takes damage they emit more and more smoke.
And what better to inflict damage on your peaceful city than an aggressive attack from outer space?
You can’t have an alien invasion without a fear-inducing alien spacecraft, which has already been added to your project – the bug-like ship at the top of your screen. This is another asset from opengamerart.org created by C-TOY called Spaceships Drakir.
The object of the game is to fly your spaceship over the buildings and bomb them into oblivion. After a few hits, a building will begin to smoke. As you continue to hit the building, more smoke will emit from it until the entire building crumbles.
Sounds easy, eh?
Well, no one likes to just take the bombing and move on with life — unless it’s good news at a gamer developers’ conference — so the population of the city defends itself with plenty of anti-spaceship missiles.
To win, your ship must dodge all the missiles and destroy every last building. And when the city is nothing but a smoking mass of rubble, you can land at the ruins of city hall and announce that you’ve come in peace.
All of the particle systems and most of the necessary scripts have been written for you. Your job is to finish the game by using some of Unity 5’s latest and greatest features.
Use the Force!
Run the game and you’ll see that nothing much is happening. An alien ship hangs low over a bored skyline. It won’t even make the 11 o’clock news. Time to spice things up.
First, select Spaceship in the Hierarchy and add a New Script component to it. Make it a C Sharp script named ShipController.
Remember how to add a new script?
[spoiler] Here’s one way to add a script.
- Click the Add Component button in the Inspector.
- In the dialog that appears, start typing New Script in the text field. You should see the component appear in the results.
- Click the New Script result to add it.
- For the script name, enter ShipController.
- Ensure the Language value is C Sharp.
- Click Create and Add.
[/spoiler]
Double-click ShipController.cs to launch it in MonoDevelop. Just inside the opening brace of the class, add the following two variables:
private Rigidbody2D shipRigidbody;
private float force = 150.0f;
The shipRigidBody
holds a reference to a Rigidbody 2D component and the force
variable holds the value of how much force you’ll use to move the ship.
You might wonder why you need a variable to hold the Rigidbody 2D component. After all, in previous versions of Unity, all you needed to access the component was to use the variable accessor, rigidbody2D
. If there was a rigidbody attached to the object, it would be populated. If there wasn’t an object, it’d be null.
Here’s the first big change in Unity 5.
In a nutshell, Unity developers felt these accessors were too inconsistent. Accessors were provided for some components but not for others. There was no logic to explain whether a component had an accessor or not.
More importantly, these accessors created dependencies throughout the various subsystems, so in order to keep development flexible, Unity junked of all the accessors except for transforms. This means you have to now create variables for all your components except transforms.
Unfortunately, Unity appears to be using the older names, so if you do decide to name your variables after the accessors, you’ll get a warning that your local variable is hiding an inherited one.
To learn more about the reasoning behind this change, check out Unity’s excellent blog post about API Changes to automatic script updating.
Now, add the following to Start()
:
shipRigidbody = GetComponent<Rigidbody2D>();
Prepare yourself to do that frequently from now on.
Finally, add the following to Update()
:
if (Input.GetKeyDown(KeyCode.RightArrow)) {
shipRigidbody.AddForce(new Vector2(force, 0));
}
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
shipRigidbody.AddForce(new Vector2(-force, 0));
}
if (Input.GetKeyDown(KeyCode.UpArrow)) {
shipRigidbody.AddForce(new Vector2(0, force));
}
if (Input.GetKeyDown(KeyCode.DownArrow)) {
shipRigidbody.AddForce(new Vector2(0, -force));
}
The gives your ship some rudimentary controls.
Run the scene. Your ship flies great except for when it passes beyond the edges of screen, and at that point it’s gone baby gone. Time to add some invisible walls.