Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RickHurd · Apr 13, 2013 at 10:44 PM · particlesystemdestroygameobjectexplode

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jiteshvm · Apr 13, 2013 at 10:57 PM 0
Share

does the collision happen ?

avatar image RickHurd · Apr 14, 2013 at 12:57 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RickHurd · Apr 14, 2013 at 11:43 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RickHurd · Apr 14, 2013 at 12:55 AM 0
Share

Ok let me clarify.

Upon collision with the sentry bot and player, the particleSystem does not trigger.

avatar image DMCH · Apr 14, 2013 at 12:08 PM 0
Share

$$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.

avatar image RickHurd · Apr 14, 2013 at 03:50 PM 0
Share

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;
 }
avatar image DMCH · Apr 14, 2013 at 04:12 PM 0
Share
 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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges