Spotlight Team Best Practices: Object placement and physics

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:

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.

9 Comments

To anyone struggling with Unity’s gizmo especially when placing colliders, I am currently developing a plugin for Unity, that intoduces blender’s gizmo into the Unity’s editor. It basically allows you to positions objects and collisions at least three to five time faster than with the standard Unity tools. Everything the author talked about in this post (local space / global space, pivot / custom pivot, vertex snapping) is present, but it is augmented with things like the ability to only move/rotate/scale (and vertex snap) the object on X axis (global/local) and around a custom 3d cursor/pivot. Did you know you can vertex snap when rotating objects? And when scaling? And that it’s super useful (esp. with colliders)? With my plugin you can!

“We will be back next time with lessons learned working with Realtime Global Illumination and multi-scene editing.”

and

“Our next blog will be a little bit different. We will be sharing several small editor tools written in-house that will hopefully improve your productivity and show you how to customize Unity yourself.”

Would the new Physx Editor support allow you to pre-settle cloth objects?

Several years (and Unity versions) ago, I attempted to write my own tool that would do this. I got the cloth to simulate successfully in Editor, and the resulting settled cloth states to save as part of the scene. But the tension values for the cloth vertices were never successfully saved. As a result, the cloth wouldn’t behave correctly at runtime. The cloth would always sag a bit more when the game started.

Since then, there’s been a new Physx version, and all the Unity cloth code has been written, I believe. Maybe things are different now!

So, would the physics settling example at the bottom of this post also allow you to settle and save cloth the same as Rigidbodies?