• No results found

Arranging GameObjects

In document Unity for Absolute Beginners (Page 55-59)

Typically, when you are creating an environment with a lot of duplicate geometry, you will import the original and then make duplicates of it in the scene. Although it doesn’t reduce the overhead during runtime, it does reduce disk space, or in this day and age, download time.

The end result is that the modular asset must be arranged in the scene. To help with that task, Unity has a very nice vertex snap feature.

1. Arrange the view so that you can see all three cubes easily.

2. Select Cube1.

3. Hold the v key down on your keyboard, and move the cursor around the cube.

The transform gizmo jumps to the closest vertex.

3D MESH COMPONENTS

If you are new to 3D, you might not know that mesh objects have three component parts: vertices, edges, and tris (short for triangles). A triangle, sometimes called a face, is the smallest surface that can be rendered. It is defined by three vertices and the three edges that connect them (below, left). Unless you are using a two-sided shader (a “shader”

contains the code that tells the graphics hardware how an object is drawn on screen), faces are only “drawn” or

“rendered” on one side, their face normal side. The “normal” is an imaginary line perpendicular to the face that indicates its “outward” or visible side (below, right).

4. When the cursor snaps to the lower vertex closest to the next cube over, press and hold the left mouse button, and then drag it over to the next cube.

5. With the mouse button down, move the cursor around the target cube and watch the original vertex snap to its new target.

6. When you are happy with the alignment, let go of the mouse button to finalize the arrangement.

7. Repeat the process with the third cube in the row or stack you started with the first two.

You can also set objects to snap at intervals and use a rotation snap.

1. Select one of the cubes, and set its rotation values back to 0.

2. From the Edit menu, at the very bottom, select Snap Settings.

The grid snaps are set to 1 unit. This means that the object must be within 1 unit or meter of the grid intersection before they will snap. Because the cubes’ pivot points are at their centers, the cubes were originally halfway down through the scene construction grid. The first thing to do is move it up.

3. Select the cube again and, holding the Ctrl key (Windows) or the z key (Mac), move the cube up until it snaps its base to the grid floor.

4. Try snapping the cube to the grid in the Global X or Z direction.

Of interest is that the cube keeps the same offset, as it snaps 1 unit each direction. If you want it to snap to corners, you can use the buttons at the bottom of the Snap Settings dialog to center the cube on an intersection, add 0.5 to the X and Z in the Inspector, and then happily snap between corners.

Angle or Rotational snaps are set to 15 degrees as a default. You will want to set them to some number that makes sense for your needs before you snap rotate.

5. Change the mode to Rotate.

6. Hold the Ctrl (or Cmd) key down, and rotate the cube on its Y axis, noting the 15 degree increments or decrements in the Inspector.

7. Feel free to round up if the rotation value in the Inspector is infinitesimally off at the end of the rotation.

Parenting

So far, you have dealt with singular objects. It’s quite typical, however, to group multiple objects together and parent them to a single gameObject for easier handling. The most important thing to know is that children inherit the transforms of their parents. If a parent is moved two meters in the Z direction, the child is moved that same two meters. If the parent is rotated, the child is rotated relative to the parent. If the parent is scaled, the child receives the same scale, again, relative to the parent. It sounds straightforward, but it is worth looking into further.

1. From the GameObject menu, Create Other, create a Capsule.

2. In the Inspector, set its position to 0, 0, 0.

3. Select Cube1, and position it at least a couple of meters away from the capsule.

4. Select the Capsule in the Hierarchy view, and drag and drop it onto Cube1 in the Hierarchy view.

5. Check its transforms in the Inspector, and make note of them.

They now reflect its offset from its parent rather than its location in the scene.

6. Click the arrow to the left of the new parent object to see the newly created hierarchy (Figure 2-10).

Figure 2-10. The parent, Cube1, and its new child, Capsule

7. Now select and rotate Cube1.

The capsule rotates around its parent as expected.

8. Inspect the capsule’s x, y, and z Position values.

The values remain the same as when the Capsule was first parented to Cube1.

9. Double-click the capsule to frame it in the viewport.

10. Select Cube2.

11. From the GameObject menu, use “Move to View” to position it at the scene’s focal point (Figure 2-11).

Figure 2-11. Cube2 moved to the Capsule’s location

Figure 2-12. The Capsule’s Position values, left, and Cube2’s Position values, right 12. Now look at Cube2’s transform values.

As you may have expected, the two values do not match.

13. Select the Capsule in the Hierarchy view, drag it out of Cube1’s group, and drop it in a clear space below the other gameObjects.

14. Now the two objects’ position values match (Figure 2-12).

Components

Now that you’ve been introduced to the Transform component, let’s look at a few more components.

Components are the building blocks that specify the functionality for every gameObject. In addition to the mandatory Transform component, each of the primitive objects you created has a Mesh Renderer, something called a Collider of an appropriate shape for its primitive, and a Mesh Filter for its particular primitive’s geometry. When you create a Unity gameObject from Create Other, it comes complete with the components that make it what it is.

In document Unity for Absolute Beginners (Page 55-59)