2D in Unity3D - Basic Setup and Physics

With the recent update to 4.3, Unity3D has added built in support for 2D projects. It is worth noting that this does not replace the 3D features, but rather supplements them, and almost all of these features come included with the Indie Version.

To create a new project in 2D, be sure to select Set up defaults for: 2D to tell the editor to assume you are working in 2D. Don't worry, you can mix 2D and 3D or work in one even if you didn't select this option.

Unity 2d Project

 

From here, we can start exploring the new 2D features. To start off, make sure the scene is in the 2D view to make editing in the scene easier.

Unity 2D Scene View

 

BlockNow that we have the editor set up correctly, we need a sprite or two to use in the game. Even though this is not limited to pixel art, we will use a 32x32 image for our first sprite.  You can download this image by right clicking and choosing Save Image As... so you can save it and add it into your assets folder in the Unity3D project.

Once the sprite is downloaded, click on it in Unity3D to open it in the Inspector. We want the engine to treat this as the new Sprite object, so we select this option for Sprite Type. This is a single sprite instead of a sprite sheet, so we keep the Sprite Mode the same. We are not going to use Sprite Packing, so keep that blank. Since this block is 32x32 and we want it to take up 1 unit in Unity, we change the Pixels to Units to 32. Having the Pivot point at the center is sufficient for our needs, and we want to change our Filter Mode to Point so the pixels stay sharp.

Sprite Settings

 

Be sure to hit Apply!

We can now drag our newly setup sprite into the scene. From here, we will have our sprite displaying as a convenient 2D object, letting Unity handle the 3D quad behind it. One of the more convenient features of the new 2D mode is the 2D physics engine using Box2D. To test this, we can add a collider to the sprite by going to Component > Physics 2D > Box Collider 2D with the sprite selected in the Hierarchy.

Box Collider 2D Menu

 

Transformation ToolsGo ahead and duplicate the box we have made by right clicking on it in the hierarchy. Make the duplicated copy larger than the original with the transformation tools, large enough to be a shape for the original to land on. Arrange the blocks in a way that will be interesting to see, maybe rotate them by clicking off of the corners with the 2nd transformation tool.

Arranged Blocks

Finally, add a Rigidbody 2D to the smaller block from the same menu as the collider. When you hit play, the top block should fall onto the bottom, and depending on how you set it up, slide off. Congratulations! You have just made your first 2D project in Unity3D.

Comments