x


How can i spawn an explosion?

Hi, i downloaded the Explosion framework, and now i want to load it at a spawn point. I want that the user touches a cube and then the spawn point spawns the explosion.

The spawn point is called "Spawn1" The prefab "explode" Im very new with unity, an im not so good, but i have this script:

function OnTriggerEnter(other: Collider) { if (other.CompareTag("Player")) { //what i have to inseret there? } }

more ▼

asked Jan 30 '10 at 02:58 PM

Sam94 gravatar image

Sam94
11 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

From skimming the code which comes with the Detonator package, it seems you simply have to create an instance of one of the detonator prefab explosions (which are provided in the "Prefab Examples" folder), using the Instantiate function. Eg:

var exp : GameObject = Instantiate (explosionPrefab, position, Quaternion.identity);

This assumes that 'explosionPrefab' is a reference to one of the example prefabs, or one of your own custom detonator prefabs. 'position' should be a Vector3 representing where you want the explosion. You might just want to use "other.transform.position" for this value, if you want the explosion to occur at the 'other' object's location.

And then, it appears as though you also have to ensure the explosion object is cleaned up yourself too, by using something like this:

Destroy(exp, explosionLife);

Where 'explosionLife' is the number of seconds after which the explosion instance will be destroyed and removed from the scene.

For a more detailed example, just look through the "DetonatorText.js" script which comes with the package. In it, the function "SpawnExplosion()" uses a RayCast to determine where in the scene the explosion should occur (based on the mouse position), and then creates an explosion at that point. (actually at a point slightly away from the surface of what was clicked).

So, your final code might contain something like this:

// near the top, with your other var declarations:
var explosionPrefab : GameObject;
var explosionLife : float = 10;

// then later, your trigger enter handler:
function OnTriggerEnter(other: Collider) {
    if (other.CompareTag("Player")) {
        var expPos = other.transform.position;
        var exp : GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity);
        Destroy(exp, explosionLife);
    }
}

Hope this helps you get started.

more ▼

answered Jan 30 '10 at 04:32 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1273

asked: Jan 30 '10 at 02:58 PM

Seen: 4357 times

Last Updated: Jan 30 '10 at 02:58 PM