Unity Training Roundup: 2D features & tips
In recent versions of Unity, some incredible 2D tools have been added. Now that we’ve reached a critical mass of 2D features, it feels like a good time to look back over some of our most popular 2D content and put them all in one place in case you have missed anything.
After watching these videos you’ll be able to use Unity’s 2D toolset to rapidly design beautiful 2D worlds, create custom 2D physics in C#, use our powerful new sorting tools, and much more.
In case you’ve never followed along with one of our Live Sessions, each of these video playlists contains an hour long video presentation, as well as free project assets including scripts, scenes and artwork, which you can use to follow along and learn about Unity’s features, or just grab and re-use in your own projects!
Live Sessions happen on Wednesday nights GMT and people who show up for the live event are able to ask questions during the session, and stay for an extended Q&A at the end, which is usually a lot of fun. If you’ve never attended one live, we’d love to see you there! You can find the full schedule here.
Designing with 2D game tools
The first session we’ll look at is Designing With 2D Game Tools. The focus of this session was to show some of the powerful new sorting and collision tools, which were recently added to Unity. The sorting tools are particularly useful for developers who are making games in which there is any kind of forced perspective. In this case, game elements need to be able to move in front of or behind one another.
Unity already includes functionality called Sorting Layers, which allows you to define which layer is rendered in front of another, and also a more fine tuned order in layer option to define sorting within a single layer. For situations in which there is no dynamic sorting of sprites this is fine, but if we want one object to be able to move closer to the camera and then sort in front of other objects it poses a challenge.
The addition of the capability to define Axis Sorting and Sorting Groups made solving these problems much, much easier. Axis Sorting is simple, but very useful. It allows you to define an axis, which as an object moves along it will cause it to sort in front of or behind other objects.
In our video demo we used the Y axis, so objects at the bottom of the 2D screen are drawn in front of objects which are higher up on the screen. This feature combines very well with Sorting Groups. Sorting Groups allow us to group together a set of objects based on their Transform hierarchy and render them together. This means that, for example, all parts of a character that are children of the same GameObject will be sorted as one object.This avoids situations like, for example, when the head of a character is behind a tree, but the leg is in front (which looks very, very weird).
In the session below we’ll work through sorting groups, and also look at some of the powerful new 2D collider options, including the new dynamic Composite Collider 2D.
2D World building with Tilemap & Cinemachine
Our second session is 2D World building with Tilemap & Cinemachine for 2D. In this session we continue to use Composite Collider 2D, but in this case in combination with Unity’s Tilemap. Tilemap is an incredible tool for 2D games that allows you to rapidly author levels on a Grid by drawing sprites and collision data with brushes. In this training we design a small 2D platformer level using Tilemap and add collision to the tiles using the Tilemap Collider.
The Tilemap Collider adds collision to the sprites in the Tilemap based on their shape. The drawback with using it on its own is that a Collider is added to every single tile on the Grid, including tiles which nothing will ever collide with, because they’re in the middle of a wall for example. Adding the Composite Collider 2D allows us to significantly optimize this by combining all those colliders together, leaving only the relevant collision shapes, and a much smaller number of total colliders in the scene.
One of the most interesting new aspects of building worlds with Tilemap is the fact that Tiles themselves are scriptable, and during the training we will walk through a series of awesome dynamic tile tools including animated tiles, random tiles and context-aware rule tiles, which change their sprite based on what they are next to. All these tools are available separately as part of the 2D Extras Pack on GitHub, and already included in the free project which you can download to follow along with the 2D Worldbuilding session.
In addition to looking at building levels with Tilemap, we also introduce Cinemachine for 2D. Cinemachine is an incredible new camera solution from Unity, which removes the pain of writing complex camera code. Cinemachine makes designing subtle, dynamic camera systems easier than ever.
Previously, the tool was only available for 3D projects, but now we’ve updated it to include 2D support as well. In this example we’ll create a dynamic camera, which smoothly follows our character while never allowing the player to see outside of the pre-defined background area.
For a short, 3 minute video, which just looks at Cinemachine, check out our dedicated feature demo video as well.
2D Platformer Character Controller
The third session is a more advanced, programmer oriented session. This session is called 2D Platformer Character Controller. In this session we go through the process of writing a character movement script, or controller for a 2D platformer character. Unity includes a 2D physics system called Box2D which seeks to provide, for lack of a better term ‘realistic’ physics.
Objects have inertia, gravity, friction and can bounce off each other and slide around. The canonical 2D platformer games that we know and love, like for example the Mario series by Nintendo do not use realistic physics. Instead they have custom, hand-written movement and physics code which is designed specifically for the gameplay of those games. This is what we and our players have been trained over the years to feel is ‘right’ in platformer games.
That doesn’t mean this is the only way to ever make a platformer, by all means feel free to break the mold and reinvent the wheel, but if you’ve ever tried to make a good feeling platformer controller using physics and struggled with that, this probably has something to do with it.
The approach shown here hooks into Unity’s collision and overlap detection system to manage collision with the environment, but none of the actual movement of the character is handled by the 2D physics system. This allows programmers a much greater degree of control over how their movement feels, and in my opinion should come closer to a ‘classic’ 2D platformer feel.
We hope you’ve found some of this information useful for your own 2D projects and would love hear about your experiences working with our 2D tools.
Have you used any of these tools? Did you like them? Let me know in the comments below.