Game Development Reference
In-Depth Information
Land With Force
While the DustPuff particle system emphasizes the force of the player character's landing visually,
nearby objects can also be affected with a seeming dispersal of force. In the Project panel Assets ➤
Scripts folder, select Create ➤ JavaScript and name it ImpactForce. Attach it to the GrandEntrance
game object, then open it in MonoDevelop. Edit the code to the following:
#pragma strict
function Start () {
Invoke ("ImpactForce", 0.75);
}
function ImpactForce () {
var colliders : Collider[] = Physics.OverlapSphere(transform.position - Vector3(0, 4, 0), 10);
for(var cldr : Collider in colliders)
{
if(cldr.rigidbody == null) continue;
cldr.rigidbody.AddExplosionForce(10, transform.position - Vector3(0, 4, 0), 10, 0,
ForceMode.Impulse);
}
}
Save the script. This code breaks down as follows:
(1) Invoke ("ImpactForce", 0.75);
The Invoke function lets you call functions with a time delay before the
function is implemented. It takes two arguments: the function to be called,
and the time delay in seconds. In this example the force should coincide
with the player character landing on the ground and the start of the DustPuff
particle system emission, which you know from its Start Delay is 0.75
seconds.
1.
(2) (3)
var colliders : Collider[] =
(4) (5) (6)
Physics.OverlapSphere(transform.position - Vector3(0, 4, 0), 10);
Within the ImpactForce() function,
Declare a reference variable colliders
2.
for an array of type Collider .
3.
4.
Assign any colliders found within the boundaries of the
Physics.OverlapSphere
 
Search WWH ::




Custom Search