Graphics Reference
In-Depth Information
The data manager handles health, so myDataManager.ReduceHealth is called with a
parameter of how much to reduce the health by, as an integer:
// reduce lives by one
myDataManager.ReduceHealth(1);
If the player's health is less than 1, the player needs to explode and respawn. First,
isRespawning is set to true, to avoid duplicate respawn calls. Next, the script adds an
explosion force around the player. If there are any other players (or physics rigidbodies)
around when the player is hit, they will be pushed away from the impact point:
if(myDataManager.GetHealth()<1) // <- destroyed
{
isRespawning=true;
rigidBody.AddExplosionForce() takes three or four parameters: the explosion force
as a float value, a Vector3 position of the explosion, and the radius of the explosion, as a
float. The final parameter is an optional one: a float to act as an upward modifier, making
the explosion force upward as if it were from beneath the object.
The Unity documentation has this to say about AddExplosionForce():
Applies a force to the rigidbody that simulates explosion effects. The explosion force will
fall off linearly with distance to the rigidbody.
The function also plays nicely with ragdolls.
If radius is 0, the full force will be applied no matter how far away position is from
the rigidbody. upwardsModifier applies the force as if it was applied from beneath the
object. This is useful since explosions that throw things up instead of pushing things to
the side look cooler. A value of 2 will apply a force as if it is applied from 2 meters below
while not changing the actual explosion position. explosionPosition is the position from
which the explosion force is to be applied. explosionRadius is the radius of the explosion.
Rigidbodies further away than explosionRadius will not be affected.
In this case, the explosion force is going to be a huge amount for dramatic effect: the
mass of the player vehicle multiplied by 2000. The explosion is at the center of the vehicle,
and the blast has a radius of 100:
// blow up!
myBody.AddExplosionForce(myBody.mass * 2000f,
myBody.position, 100);
To help the drama of the explosion, the next line makes the vehicle's rigidbody spin in
random directions. rigidbody.angularVelocity is set to a Vector3 containing three random
amounts from −100 to 100:
myBody.angularVelocity=new Vector3( Random.Range
(-100,100), Random.Range (-100,100), Random.Range
(-100,100) );
The game controller deals with making explosion particle effects, so the next line tells
GameController_MVD about the hit:
Search WWH ::




Custom Search