Search Unity

Spotlight Team best practices: Object placement and physics

January 3, 2018 in Engine & platform | 6 min. read
Topics covered
Share

Is this article helpful for you?

Thank you for your feedback!

On the Spotlight Team, we work with the most ambitious Unity developers to try to push the boundary of what a Unity game can be. We see all sorts of innovative and brilliant solutions for complex graphics, performance, and design problems. We also see the same set of issues and solutions coming up again and again.

This blog series looks at some of the most frequent problems we encounter while working with our clients. These are lessons hard won by the teams we have worked with, and we are proud to be able to share their wisdom with all our users.

Many of these problems only become obvious once you are working on a console or a phone, or are dealing with huge amounts of game content. If you take these lessons into consideration earlier in the development cycle, you can make your life easier and your game much more ambitious.

Putting that thing where you want it

Despite being one of the most common things done in Unity, we see lots of teams struggling with the best way to move an object around. Specifically, it can be tricky to get objects to line up perfectly so that characters stay in the world, paths get built properly, and physics objects rest nicely on each other.

Unity provides quite a few tools to make this easier, but they all can serve different roles. These tools are necessary to understand once you start trying to make lots of consistent content or try to make scenes like the one above that are dense with dynamic physics objects.

The right Gizmo for the job

We provide tools for Translation, Rotation, Scaling and, new in 2017.3, a universal tool that will let you do all three. Each of these Gizmos lets you alter the transform properties of your game object in 3D. Additionally we have the Rect Transform tool for working in 2D.

All of these Gizmos operate based on the mode toggles that follow them. If you have your Gizmos set to Center, you will be working with the calculated middle of the object based on its bounds. This is an approximation, so may feel a bit off of true center for complicated objects. When your Gizmos are set to Pivot, you are operating on the (0,0,0) point of the object in its own frame of reference. This Pivot, hopefully, is set at a useful point by whomever created the asset in the first place. In general, we recommend operating in Pivot mode when placing objects relative to each other and keeping the Pivot location in mind when creating your assets. The second toggle, Global / Local, controls what space you are operating in. Global sets your gizmos to align with the world (x,y,z) axes, while Local sets them to the object’s own (x,y,z). Personally, I mostly work in Local space. If I am trying to get multiple objects to align to each other, however, then I will often work in Global coordinates and use the grid snapping to get things to line up nicely.

Snap!

The most straight forward positioning tool in Unity is holding the CTRL (Win) or Command (OSX) key to snap to a grid. Any time you are moving an object by its Axis handles (those blue red and green gizmos), holding down the snapping key will cause the object to move in increments of your current grid size... which you can change!

The Snap Settings menu lets you set per axis values for your grid size, gives you control of snapping for both rotation and scale, and lets you round a selected object’s current transform to the nearest grid line.

The Snap All Axes button is particularly useful for getting arbitrary content lined up. You can select a group of objects, snap them to the grid in all axes, and then reposition them so they line up perfectly. Like so:

This works well for regular geometry, large scale level layout, and putting in blocks of collision. If you have more complicated placement needs however…

Vertex Snapping

Press and hold V and the Transform Gizmo will switch to vertex snapping mode, as indicated by the little square. You can then click on a vertex of your selected object’s mesh, and then drag it to snap to any vertex under your pointer. This is very useful for aligning arbitrary meshes, especially those with complex or non-existent collision. Neither the wire or the lamp above have anything like a tight collision mesh, and neither are even vaguely cube-like, so they don’t fit together with grid snapping or collision snapping.

You can also turn on Vertex snapping mode if you are going to be placing a lot of Game Objects this way. Hit Ctrl+Shift+V to turn on and Ctrl+V to turn back off. This lets you vertex snap a lot of objects without holding down the V key the whole time.

Collision Snapping

If you hold down CTRL + SHIFT, you enable Collision snapping. You have to then click and drag on the little yellowish square at the center of the Transform Gizmo, rather than using the axis controls. This will then cause the selected object to try to line its collision up with the collision of whatever is under the cursor.

Lining up meshes with vertex snapping or grid snapping works great for static geometry, but if you have dynamic objects, objects that might, say, interpenetrate and launch themselves into space when touched, you really want to snap not to the visible mesh but to the collider around it.

Collision Settling

If you have lots of dynamic physics objects, or very complicated joint system, it might not be feasible to use collision snapping to get everything where physics says it should be. Fortunately, we now allow you to run PhysX in the editor. This lets you simulate a few seconds of game time, let your physics come to a rest, and then save out the resulting positions.

You can find the heavily commented example code complete with debug drawing and a menu item here. Just throw that file into any “Editor” folder in your project and you should get a new GameMenu->Settle Physics menu item.

Knowing when and why to use grids, vertex snapping, collision snapping in your level design workflow will come with experience. Play around with these tools, along with the Pivot vs Center and Global vs Local toggles until you find what works for you. The less time you have to spend arranging your virtual furniture, the more time you can spend finding the fun. Big thanks to Campo Santo for letting us use their assets for our examples.

January 3, 2018 in Engine & platform | 6 min. read

Is this article helpful for you?

Thank you for your feedback!

Topics covered
Related Posts