Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Making an object kill itself on collision

Discussion in 'Scripting' started by Deleted User, Mar 5, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have a Fireball and upon collision, I want it to die. Up until now I did something like this:

    (Freehanded)
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other){
    2. gameObject.Die();}
    However, because collisions are not processed simultaneously, what would happen is the first Fireball would kill itself, and the 2nd one would remain alive because no collision was detected (since the first Fireball which it collided with died).

    I'm thinking of doing something like this:

    (Freehanded)
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other){
    2. if (willDieOnCollision){
    3. gameObject.Die();}
    4. other.gameObject.Die();}
    But I'm not sure if that's the best way to do it? Ideally, there's a lot more to just killing the projectiles that'd I'd like to do.
     
    Last edited by a moderator: Mar 23, 2015
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Send a message to both objects to die.
     
  3. Deleted User

    Deleted User

    Guest

    One problem I have is that sometimes, the other object shouldn't die though. Not every projectile should die on contact.
    In that case, right now I'm checking to see whether the other object should die from the colliding object. I'm not sure if that's the best way to do things because I'd like each object to be responsible for itself.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Depends what your dieng looks like. You you want to compare force or something else?As long as both objects collide, you can do whatever you want with them.
     
  5. Deleted User

    Deleted User

    Guest

    I should have said this from the beginning.
    Let's say I have 2 Fireballs: Big Fireball that has 40 damage and Small Fireball that has 20 damage.

    When a Big and a Small Fireball collide I want Big Fireball's damage to be its current damage (40) minus Small Fireball's damage (20). I want Small Fireball's damage to be its current damage (20) minus Big Fireball's damage (40).
    So the resulting collision should result in Small Fireball dying (because it's damage is less than 0) and Big Fireball's damage to be reduced to 20.

    The thing is, I want to make sure the collision doesn't get run twice.
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    OnCollisionEnter does not get called twice on the same collision. Just call a function on the fireballs that lowers their hp and check if their HP is below 0. Then destroy or disable them when needed.

    Edit: OnTriggerEnter is not what you want here. You want two objects to collide with each other. Use OnCollisionEnter. Make sure both objects have a rigidbody and set trigger disabled.
     
  7. Deleted User

    Deleted User

    Guest

    I'm using OnTriggerEnter and if two objects collide and they each have a "OnTriggerEnter" function then it will be called on both function but only if the other colliding object is still alive by the time the 2nd OnTriggerEnter is called.
     
  8. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Triggers are not ideal in this situation. I doubt both triggers will be called in a collision.
     
  9. Deleted User

    Deleted User

    Guest

    What's wrong with triggers? And yes, both triggers are called in a collision.
     
  10. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    When you add colliders to game objects by default they will collide with each other. This triggers a collision event, which then can be handled with OnCollisionEnter() and when they stop touching each other the OnCollisionExit handler will get called. Colliders used in this fashion will collide with each other but cannot pass through each other.

    If you change the collider to be a trigger then the game objects can not only "collide" with it but can pass through it. This is why triggers are best used for zones that trigger something, for instance walking into a collider set as a trigger could switch a light on, or save the game data, etc...

    Do you want the fireballs to be able to pass through each other when there is a collision? If so, then setting their colliders to being triggers is the way to go, and then you would have your logic in OnTriggerEnter and OnTriggerExit.

    If you do not want them to be able to pass through each other, but rather simply collide, then you should not set them as triggers and use OnCollisionEnter and OnCollisionExit.
     
  11. Deleted User

    Deleted User

    Guest

    @rnakrani Thanks but that's not the issue. The issue is let's say I have 2 fireballs that collide with each other and kills itself on contact.

    Fireball 1
    oncollision{
    die}


    Fireball 2

    Fireball 1 is dead so no collision registered
     
  12. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Try something like this. You will need to give the colliders a bit of time to register before destroying them. But you can disable the renderer to make it look like they were immediately destroyed.

    Code (CSharp):
    1. public IEnumerator delayDestroy(){
    2.  
    3. yield return new WaitForSeconds (0.01f);
    4. Destroy(gameObject);
    5. }
    6.  
    7. OnTriggerEnter(Collider other ){
    8.  
    9. damage = damage - other.GetComponent<scriptName().damage;
    10.  
    11. if(damage <= 0){
    12. renderer.enabled = false;
    13. StartCorutine("delayDestroy");
    14. }
    15.  
    16. }
     
    Deleted User likes this.
  13. Deleted User

    Deleted User

    Guest

    Thanks. Seems like a good workaround but I'd also have to disable the script my collision code is in, right? Otherwise if two projectiles are very close to each other could cause some issues.
     
  14. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I think it should be fine. What kind of issues are you thinking of?
     
    Deleted User likes this.
  15. Deleted User

    Deleted User

    Guest

    No you're right it shouldn't cause any issues.