Game Development Reference
In-Depth Information
The first thing you can notice in the script is the relatively high magnitude of
ex-
plosionForce
, which is reasonable since we are talking about an explosion that can destroy
a building. The radius in which the explosion takes effect is set by
explosionRadius
, which
means that only objects within this distance are affected by explosion force. This script
creates an explosion when a block is clicked. However, to make the explosion more obvious
and effective, we move for a short distance from the block position and perform the explo-
sion there. This distance is determined by
explosionPosition
, which is relative the position
of the target block that was clicked.
OnMouseClick()
handlesmouseclickonthedestructibleblockbyaddingexplosionforcetoallsurrounding rigid
body within the given radius. First step is to get all rigid bodies and storethem in
allBodies
array. After that,
explosionPos
is computed by adding
explosionPosition
relative position
to the position of the block. The
for
loop in line 37 iterates over all rigid bodies and
finds the distance between each body and the computed
explosionPos
. If this distance is
less than
explosionRadius
, we send
Destruct
message to the rigid body and then call
Ad-
dExplosionForce()
.
AddExplosionForce()
takes 3 arguments: the strength of the explosion
which is
explosionForce
, the position of the explosion which is
explosionPos
, and the ra-
dius of explosion effect which is
explosionRadius
. The importance of sending
Destruct
message is to unfreeze the rigid body, in case it has
Destructible
script attached to it.
Without this step, the explosion isn't going to have any effect on the rigid body. Illustration
73 shows a demo explosion. Notice that the effect of the explosion is purely physical, and
has no visual effects such as fire or smoke.