Search Unity

Resolved Issues detecting colliders within trigger collider

Discussion in 'Physics' started by PaperMouseGames, Aug 4, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there! I'm working on a top down style RPG akin to the old Final Fantasy games or Stardew Valley and I'm running into an issue regarding colliders of world objects like trees and houses.

    I'm just using
    OnTriggerEnter2D
    and
    OnTriggerExit2D
    .

    So my objective is to make these objects transparent when the player walks behind them so that it's easier to see. I almost have it working but I'm running into an issue with objects that have multiple trigger colliders. See I'm using trigger colliders on the objects in order to check if the player is in a position such that the object should become transparent. The issue comes when an object has more than one trigger collider and the player exits one of those colliders but is still in the other(s).

    Here's an image to show the issue:
    Sprite-0001.png
    So the colliders are the green boxes, and the player is the red box.

    So when the player moves from position 1 to 2, he exits the smaller trigger collider and the
    OnTriggerExit2D
    operations are carried out, but he's still inside the big collider so I don't want that to happen.

    Any tips on how I could remedy this? Is there a better solution than what I'm doing? Thanks in advance!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Maybe use a counter rather than just enter/exit state. Increment it when Enter and decrement it when Exit. When it transitions from 0->1 then you're entering. When transitioning from 1->0 you're exiting.
     
    PaperMouseGames likes this.
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hey, thanks for your reply! Sorry but I'm not sure I'm following. Are you saying to use a counter instead of the Enter and Exit trigger methods? I'm also not sure how the counter is supposed to help here.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Yes

    For the reason I explained. You want to know when you can make an object transparent then opaque again when you exit ALL of its trigger regions? If so then what I said above applies using the transition I described. When counter transitions > 0 make it transparent and when the counter transitions to zero make it opaque.
     
    PaperMouseGames likes this.
  5. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Oh I think I understand what you're saying, so anytime the player enters a trigger I set the counter up and then down when he exits. The part I find confusing is, that I still need the enter and exit methods for that don't I? Otherwise how would I know to do the counter up or down.

    The other option I thought of is using a polygonCollider but I worry that would be too inefficient since this technique is for things like houses and trees which there are a lot of.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Yes but not sure why that's confusing. You didn't say you didn't want to use them.

    There are other queries like IsTouching/IsTouchingLayers/GetContacts which all query existing contacts. You can use these to determine if you're in contact with something. Maybe only check it when you get an exit. That way you only do so to determine if it's not touching at that point.
     
    PaperMouseGames likes this.
  7. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Thanks for the help with this, I'm using a counter in conjunction with
    OnTriggerEnter2D
    and
    OnTriggerExit2D
    now and it's working as desired!

    Sorry for the confusion, I think I just thought you were suggesting I replace those methods with a counter and so I was scratching my head wondering how a counter determines collisions haha

    Anyway, thanks again!
     
    MelvMay likes this.