- Home /
How do i get my object to explode and destroy?
So I have a sentry bot that slowly creeps up on you and I want it to explode when it collides with the player. I have a particle system I wanna use as the explosion, and wrote a script to make it explode and destroy the sentry bot. However its not working...
var explosion : ParticleSystem;
function OnCollisionEnter(){
var expl = Instantiate(explosion, transform.position, Quaternion.identity);
Destroy(gameObject);
Destroy(expl, 3);
}
any pointers?
yes collision happens, the sentry bot stops the player from moving, but then the sentry droid just gets closer and closer till the pivots are in the same spot and then spins around. The particleSystem does not trigger and the sentry bot doesn't die/go away
Answer by zero_rogue · Apr 14, 2013 at 06:59 AM
The Script is sound, My guess is the collision is not registering. I'm guessing neither your player or your bot have rigidbodies. Try adding a Rigidbody to one. Additionally I'd suggest Turning the collider into a trigger. This kind of thing is what triggers are made for and will give you more flexibility. Just enabled Trigger on the bot's collider and change OnCollisonEnter to OnTriggerEnter
So I don't have rigidbody on the "Player" as I have a character controller on it, and they conflict, however I went ahead and tried combonations of rigidboy/mesh collider on each the sentry bot and the "Player". When I have trigger checked on the sentry bot and change the script to OnTriggerenter, the player passes right through the sentry bot and no ParticleSystem event. I had no issue colliding with the sentry bot before, just no explode(ParticlePystem) and no death.
The error I get once I am back out of testing, is
Script error: OnTriggerEnter
This message parameter has to be of type: Collider
If I am not having trouble with collision, then why is my ParticleSystem not keying? With my original code, the player and Bot will collide and stop for a second while the bot just gets closer(clipping through the player) and then spins.
Answer by DMCH · Apr 13, 2013 at 11:48 PM
Hi Rick. You'd need to give us a little more detail so we can figure out the problem - it's not working is kinda vague. Looking at the code, I think the sentry bot will explode the moment it collides with anything, so you'd need to specify the player. The code (not tested) would go something like this:
function OnCollisionEnter(collision : Collision)
{
// Check the bot has collided with player
if(collision.gameObject.tag == "Player")
{
// Instantiate Explosion
Instantiate(explosionPrefab, pos, rot);
// Destroy the gameobject
Destroy (gameObject);
}
}
Destroying the gameobject MAY interfere with the instantiated explosion, so you may need to work around this. Also, if you set your particle emitter to one shot, you won't need to call destroy on it.
Ok let me clarify.
Upon collision with the sentry bot and player, the particleSystem does not trigger.
$$anonymous$$ight be a good idea to add a debug statement in on Collision to make sure the code is being run. Is the sentry bot being destroyed? Is it possible you have isTrigger ticked on your collider? If you do, it would fire a trigger event, rather than a collision.
isTrigger is not ticked, I have collision with the "Player" and the sentry bot, just no particle system and then death.
Where should I put the Debug.Log ("Boom"); ?
var target : Transform;
var moveSpeed = 4;
var rotationSpeed = 5;
var myTransform : Transform;
var explosion : ParticleSystem;
function Awake(){
myTransform = transform;
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Player")
{
Instantiate(explosion.position.rotation);
Destroy (gameObject);
}
}
function Start(){
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
var lookDir = target.position - myTransform.position;
lookDir.y = 0;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Player")
{
Debug.Log("OnCollisionEnter: Boom");
Instantiate(explosion.position.rotation);
Destroy (gameObject);
}
}
We want to check if instruction to instantiate the particle explosion and destroy the gameobject is being run, so inside the if block.
Just checking, but you have the player tagged as "Player", and a rigidbody on both the player and the sentry bot.
Think it would be worth commenting out your existing code, and trying zero_rogues suggestion of changing it to trigger. Just remember to tick isTrigger on the sentry bot's collider.
The difference between OnTrigger and OnCollision is that OnCollision makes the collider "solid", and OnTrigger allows objects to pass into the collider. So if, you are using a second collider on the sentry to detect when the player is in range you should use OnTrigger.
First, figure out if the debug statement is being printed, and then try the onTrigger solution from zero_rogue. If that doesn't work, get us some screen grabs from the inspector for player and sentry bot, and we'll see what we can do : )
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to emit particle from a collision? 2 Answers
Code for destroying object with particle and move to the next object. 1 Answer
How to adjust particle system (or Dust Storm if possible) properties from script? 1 Answer
Main Camera problem between two scenes 0 Answers