Game Development Reference
In-Depth Information
When
OnRaycastHit
message is received, this script applies an impulse force of the rigid
body once. The function used to apply the force is
AddForceAtPosition()
, which allows us
to specify a point to apply the impulse on. This addition is important to give the realistic
behavior we expect. For example, when the shot is near the upper side of the hit object, the
upper side must absorb the greatest amount of the hit which might cause object to rotate.
We take
hit.point
as the position where we want to apply the force. The magnitude of the
force is the scaled damage stored in
hit.distance
, and the direction is inside the object
-
hit.normal
(remember that
hit.normal
points outside from the object). You can experement
the full functional physics character as well as ray cast shooting in
scene17
in the accom-
panying project.