Game Development Reference
In-Depth Information
performance than another. Find these in the documentation, experiment with them, and cross-search
in the Unity community forums to rapidly expand your game developer repertoire.
Meanwhile, take a look at what's going on in the Hierarchy—a Sphere(Clone) appears for each new
instantiation. Each of these is being tracked by the physics engine, which will ultimately bog it down
and detrimentally affect game performance.
In Chapter 4, you addressed this with the Destroy() function. Then, you used the Destroy()
function's time-delay argument. Add Destroy(rocketInstance, 2); to the end of the Update()
function in the RocketLaunch script, save, and play. As expected, the rockets disappear after three
seconds. The whole point of firing rockets is to blow something up. How about hitting some targets?
Physical interactions between game objects is where colliders come into play.
Colliders
While a rigidbody component enables a game object to respond to physical forces, a collider
component enables a game object to react when it comes into contact with other game objects.
Unity's primitive objects come with collider components already attached. Select Cube in the
Hierarchy, and you'll see the green outline of the Box Collider in the Scene view.
You can add a collider either in the Inspector by selecting Add Component ➤ Physics then selecting
the type of collider, or with the editor top menu by selecting Component ➤ Physics, then the
collider type.
The Mesh Collider selection will take the shape of the game object's mesh. While it sounds ideal,
mesh colliders often have a negative effect on game performance and Unity has a limit on the
number of polygons allowed. The solution is to use several primitive colliders to approximate the
shape of your 3D model.
Note Best practices call for using mesh colliders for scene geometry and compound primitive colliders for
moving objects.
The collider properties are pretty straightforward (Figure 6-3 ). Using a collider as a trigger will be
explained in the Triggers section of this chapter. Physic Materials define in more detail the behavior
of the object when it collides. You can easily picture how a metal ball, a rubber ball, and a soap
bubble would respond differently to collisions with the same object even though each is a simple
sphere. Physic Materials will be covered in a section by that name later in this chapter. Center is
the (x, y, z) coordinates of this collider in local space. This means you can offset the collider from
the game object. The size doesn't have to be the same as the game object, either, so the remaining
fields pertain to resizing the collider depending on which primitive type you have—Size with x, y, z
values for a box; radius and height for a capsule; and radius for a sphere.
 
 
Search WWH ::




Custom Search