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

Question How do I stop a projectile with a Tilemap wall?

Discussion in '2D' started by paultoth, Nov 15, 2022.

  1. paultoth

    paultoth

    Joined:
    Mar 30, 2021
    Posts:
    15
    I currently have a projectile system which is able to detect collisions with entities possessing Capsule or Box 2d colliders, but I haven't been able to get Tilemap colliders working yet, as you can see in the attached gif.

    2022-11-14_23-09-00_AdobeExpress.gif


    Below here is the current code I have to handle my projectile's collisions. I run a switch/case statement when a collider is encountered by OnTriggerEnter2D to decide how to handle the collision based on the collider's tag (enemy, terrain, or other objects). However, this doesn't work for the terrain case as I've found that OnTriggerEnter2D doesn't work with Tilemap colliders. Additionally, I use ray casting to detect collisions for my player, but I don't know how to implement that for the projectiles since I mostly just got the code from another guide.

    With this issue in mind, what are some standard ways to stop projectiles with tilemap walls?


    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collider) {
    2.         Debug.Log(collider.tag);
    3.         switch(collider.tag) {
    4.             case "Enemy":
    5.                 if (debug) {Debug.Log("Enemy Case");}
    6.                 EnemyCollision(collider);
    7.                 break;
    8.             case "Terrain":
    9.                 if (debug) {Debug.Log("Terrain Case");}
    10.                 TerrainCollision(collider);
    11.                 break;
    12.             case "MiscEntity":
    13.                 // MiscEntityCollision();
    14.                 break;
    15.             default:
    16.                 break;
    17.         }
    18.     }
    19.  
    20.     private void EnemyCollision(Collider2D collider) {
    21.         Enemy enemy = collider.GetComponent<Enemy>();
    22.             if (enemy != null) {
    23.                 HitTarget(); // Hit target plays the projectile explosion anim and destroys the object
    24.                 enemy.TakeDamage();
    25.             }
    26.     }
    27.  
    28.     private void TerrainCollision(Collider2D collider) {
    29.         HitTarget();
    30.     }
    Thanks for any help that can be provided!! Let me know if additional info would be useful.

    P.S. Extra bit of info, here's my tile map setup. I have a separate floor and wall layer on my grid with tilemap collider 2D on the walls.
    upload_2022-11-14_23-33-44.png
     
  2. paultoth

    paultoth

    Joined:
    Mar 30, 2021
    Posts:
    15
    I figured out how to do it myself!!

    I found that I can use a Physics2D.Raycast() with a filter for my Terrain layer to detect when a projectile comes very close (0.001 distance here) to the walls and run my explosion script.

    I just threw this is my projectiles Update() method and it works!!

    Code (CSharp):
    1. private void TerrainCollision() {
    2.         RaycastHit2D hit = Physics2D.Raycast(transform.position, shootDir, 0.001f, 1 << LayerMask.NameToLayer("Terrain"));
    3.         if (hit) {
    4.             HitTarget();
    5.         }
    6.     }
     
  3. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    @paultoth Hi, glad to hear that you have found an alternate solution!

    Could you share more details on how your projectile is set up and the tag details of the TilemapCollider2D GameObject? OnTriggerEnter2D should work with the TilemapCollider2D. Thanks!
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Is the bullet collider a trigger? Cause if it isn't, OnTriggerEnter2D will not fire, OnCollisionEnter2D will though.
     
  5. paultoth

    paultoth

    Joined:
    Mar 30, 2021
    Posts:
    15
    @Chris-Trueman the bullet collider is a trigger!

    @ChuanXin Here's the full inspection information for my TileMap walls...
    upload_2022-11-19_1-5-30.png

    I tested a few different configurations here. With IsTrigger = true also didn't work with the additional issue that it broke my players collisions with the wall.

    And I could not get OnTriggerEnter2D to work with the walls in any way. Placing a Log statement in the "Terrain" trigger section never activated. I'm just planning to stick with the projectile RayCast system I have set up now, and I might revisit it later on if I find it causing issues on other aspects of the game!