Game Development Reference
In-Depth Information
Chapter 6
Starting with Coding Physics
Working with game physics is fun, and Unity's physics engines make it super easy. Physics defines
physical properties of how objects interact with each other or in response to external forces. Unity's
powerful physics engines allow you to assign physical properties to your game objects for consistent,
realistic gameplay. There are two physics engines, one for 2D and another for 3D. Though they are
implemented with different components such as Box Collider 2D and Box Collider 3D, they work
much the same way. In this chapter, you will create a simple game to learn how to set up the physical
properties of 3D game objects, then add scripting for more complex, interactive behavior.
Rigidbodies
A game object must have a Rigidbody component for the physics engine to recognize it. The physics
engine does the calculations on how the object will behave, resulting in a change in the object's
Transform. Rather than manipulating the position and orientation of the object directly with the
Transform component's translate and rotate properties as you did in Chapter 4, this time you will
manipulate the object with physical forces.
Note While you could change the Transform itself, this might interfere with the game engine's calculations
and result in unexpected behavior—just don't do it.
Start a new project with a new scene. Add a directional light, a plane with a Transform position of
(0, 0, 0), then a Cube game object with a Transform position of (0, 3, 0). Playing demonstrates a
static, primitive cube above a plane. Add a Rigidbody component to the Cube either with the top
menu by selecting Component ➤ Physics ➤ Rigidbody or in the Inspector with Add Component ➤
Physics ➤ Rigidbody. Play, and the cube falls to the plane because it is now affected by gravity.
157
 
 
Search WWH ::




Custom Search