Game Development Reference
Step 14. Save and play. A click in the Game will create a
sphere at the click location and project it in the direction the
camera is facing. Before a force is added to push the sphere,
the AddComponent() function is used to add a Rigidbody
component to the sphere as it is not attached by default. The
ScreenPointToRay() takes the mouse click location and turns it into
a vector (called a ray) that starts at the click location and continues
in the direction the camera is facing. This direction is used as the
push force on the sphere, propelling it forward.
Step 15. Gravity still affects the sphere. To see it in action, change
the multiplier for the AddForce() function from 1000 to 100.
2.5.3 The Second Law of Motion
The acceleration produced by a particular force acting on a body
is directly proportional to the magnitude of the force and inversely
proportional to the mass of the body.
This means that a larger force is required to move a heavier object. In addition,
a heavier object with the same acceleration as a lighter object will cause
more destruction when it hits something as the force will be greater. Imagine
throwing a bowling ball and a tennis ball at a wall with the same acceleration.
Which one is going to leave a bigger hole?
Unity Hands On
Step 1. Begin by opening the Unity project from Section 2.5.2 . Open
the fire script and ensure that the AddForce() multiplier is set to 1000,
for example, AddForce(ray.direction * 1000) .
Step 2. In the Hierarchy, select each cube in turn, locate the
Rigidbody component in the Inspector, and set the Mass to 10.
Step 3. Press play. Note that the heavier cubes are now more difficult
to knock over with the sphere.
Step 4. Edit fire.js to increase the mass of the sphere as shown
in Listing 2.13 . Save and play.
Listing 2.13 Script to Change the Mass of a Rigidbody
function Update ()