Game Development Reference
In-Depth Information
1. using UnityEngine;
2. using System.Collections; 3.
4. public class MouseExploder : MonoBehaviour { 5.
6.
//How strong is the explosion?
7.
public float explosionForce = 40000;
8.
9.
//Range of explsion effect
10.
public float explosionRadius = 5;
11.
12.
//Position of the explosion
13.
//Relative to the position of the object
14.
//Expressed in world space
15.
public Vector3 explosionPosition = new Vector3(-1, 0, -1);
16.
17.
void Start () { 18.
19.
}
20.
21.
void Update () { 22.
23.
}
24.
25.
//Perform explosion on mouse click
26.
void OnMouseDown(){
27.
//Get all rigid bodies
28.
Rigidbody[] allBodies = FindObjectsOfType<Rigidbody>();
29.
30.
//Calculate the position of the explosion
31.
Vector3 explosionPos = transform.position;
32.
explosionPos += explosionPosition;
33.
34.
//Find bodies within radius, send destruction
35.
//message in case they are destructible,
36.
//and finally apply explosion force to them
37.
foreach(Rigidbody body in allBodies){
Search WWH ::




Custom Search