Search Unity

Sprite with attached Particle System not working with OnCollisionEnter2D (Collision2D other)?

Discussion in 'Scripting' started by Paratrooper82, Feb 15, 2018.

  1. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13
    Thank you in advance.

    I am using the tilemap and set it up accordingly. (Tilemap Collider2D used by Composit Collider2D) This according to the unity tutorial has a static rigidbody.

    This, however, is where I am running into issues.

    I have a cannon that shoots a sprite, in my case a projectile that hurts the player. When the projectile is forced forward on the X axis it destroys over time and also destroys itself on collision. When the projectile collides with my Player it does what its supposed to: Hurt the Player, kick him back and instantiate a particle explosion. However, no matter what I try, when the projectile sprites collides with my ground tiles (created with brush through tilemap), it destroys itself, but it simply won't play the particle explosion.

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other){
    2.  
    3.         if (other.tag == "Wall") {
    4.            
    5.             Destroy (this.gameObject);
    6.         }
    7.  
    8.         if (other.tag == "Player") {
    9.             Instantiate (CannonSplosion, other.transform.position, other.transform.rotation);
    10.             Destroy (this.gameObject);
    11.         }
    12.     }
    Even when I put the same Instantiate code inside of the if(other.tag == "Wall"), it still won't play the particle explosion. Is there something I am misunderstanding?
     
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    Well, from your code I see no reference to the ground tiles or is "Wall" == "Ground"? If so there is no instantiate inside of if(other.tag == "Wall"). Also, how is your ground tiles setup? Are they very large compared to the player? It could be that they are playing but are being played behind the ground tile and therefore being occluded from view.

    I personally would change other.transform.position to transform.position. That way your explosion is instantiated at the intersection of your projectile and object instead of at the origin of the other collider.