2D Top-Down in Unity

Unity has several tools that make it easy to produce sprite based 2D games, which is what I have decided my game Kleptology will be. By default, however, Unity’s 2D tools are configured to handle side-scroller platformers (like Mario), whereas my game is a top-down perspective (like Zelda).

When you switch to 2D mode, the camera faces the XY plane along the Z axis and renders an orthographic projection. This means that by default, the X axis represents the horizontal dimension, the Y axis represents the vertical dimension, and Z is used for stuff like rotation or it can be used for visual layering effects based on their relative distances from the camera. If you decide to use the physics engine, that means the 2D physics engine uses the Y axis for gravity calculations.

The question of whether I should use the physics engine at all did cross my mind initially. It’s a sensible question since in the past I would have had to find an engine to use, or roll my own (no thanks).

If your game has continuous movement, you will likely want to use the built in physics engine. You get it for free, and it is the recommended way to implement movement in Unity. If you don’t need it, then don’t use it. Some types of games like puzzle or those with with specific movement rules might be able to go without it.

So I want to use physics, now what? Well, in 2DTD, the XY plane represents the horizontal movement plane, and Z represents the vertical component. This is not how the physics engine works by default.

In the project settings, it appears you can alter the force applied for gravity in each dimension, but let’s think about it for a second. How do you represent vertical movement in a 2DTD game anyway? There’s actually a few ways I could think of. One would be to grow or shrink the size of objects as they change elevation. Another would be what I decided to do, and how Zelda does it, which is to simply draw the sprite some relative distance along the Y axis. I discovered quickly this only works with a shadow underneath the object to indicate where it actually is in the XY plane. I will probably devote a post to how I solved this.

So what does that mean? Well, there’s a few settings you might want to look at under Edit -> Project Settings … -> Physics 2D . I set the Gravity value of Y to 0, effectively disabling gravity calculations.

In order for objects to participate in the physics engine, RigidBody2D is the behavior your GameObject will need. In 2DTD, and my game in particular, there are a few settings to tweak there also.


The values here will probably vary from object to object depending on what behavior you want, but for the most part, for all of my objects, I will set the Gravity Scale to 0, since I won’t be using it anyway, and Freeze Rotation in the Z dimension. Freezing the Z is a choice I made for my game because the orientation of objects and characters will only be important in the four cardinal directions for the most part. Allowing objects to rotate may be important for your game, but this simplification will still allow the desired effects in Kleptology (most surfaces will be aligned along the X or Y axes anyways).

And keep in mind, just because I am preventing the physics engine from rotating objects, that doesn’t mean I can’t rotate things myself. Simple rotation can be scripted easily, or the sprites themselves can be animated to appear to rotate.

That’s it for now. In my next post, we’ll move the gremlin around with physics!

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *