Game Development Reference
In-Depth Information
4.5 Physics projectiles
In section 3.1 we implemented a simple projectile system, which consisted of objects that
move with constant speed over the time. In this section we learn how to use rigid bod-
ies with impulse forces to create more realistic projectiles. Turning any rigid body into a
projectile is as simple as adding an impulse force with specific direction and magnitude to
it. In this section, we are going to create a ball that has a rigid body. This ball is going to be
thrown on a stack of boxes in a mechanic similar to Angry Birds. Illustration 64 shows the
scene we need to setup for our projectile demo.
Illustration 64: A scene to demonstrate physics projectiles
To launch the ball (i.e. the projectile), the player has to hold the left mouse button on it
and drag to the left. As indication, a line will be drawn between the ball and the mouse
position. The longer this line is, the greater is the impulse force that launches the ball. This
simple mechanism allows the player to control both the direction and the magnitude of the
force that launches the projectile with ease. One more important note: even the scene is
constructed in 3D, we are going to limit both movement and rotation in 2D. This means that
we must freeze the movement on z axis for all objects, as well as freezing rotation around
x and y axes. Not only that, we have also to make sure that all objects in the scene have
the same z value for position, to make sure they collide with each other. Additionally, we
need to add a new component called Line Renderer to the ball. This component draws a 3D
line in the space that passes through a provided list of positions in the space. Illustration 65
shows the properties of the line renderer we need to add to the ball, which is going to be
the indicator of launch direction and force.
Search WWH ::




Custom Search