Search Unity

Creating a sensor array with Physics2D.OverlapBoxAll the right approach?

Discussion in 'Physics' started by polypixeluk, Aug 3, 2020.

  1. polypixeluk

    polypixeluk

    Joined:
    Jul 18, 2020
    Posts:
    53
    I'm working on a little game where the player moves across a grid of different colored squares. The player can switch color to match the color of squares and when they overlap a square of the same color they accrue points but when they overlap a square of a different color they lose life. I was hoping someone could give me some advice / feedback on the best way to approach the code for this.

    Currently I Start() with a Coroutine that triggers a StatusUpdate method every 0.5 of a second. The StatusUpdate uses Physics2D.OverlapCircleAll to see how many tiles the player shares and/or contrasts color with and then it updates the Score and Health accordingly on a per tile basis.

    However I don't think this is sensitive / accurate enough. You can be barely touching a tile of the wrong color and be suffering the same health loss as if you were completely within its boundaries and score has the same problem.

    I was thinking rather than a single overlap check maybe it would be better to create a 4x4 or 8x8 grid of Physics2D.OverlapBoxAll checks that act like a sensor array and give me at least some crude indication of how overlapped the player is. Does that sound like a sensible approach baring in mind this is for mobile? If not how would other people approach this kind of a situation?

     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    First I wouldn't use the "All" or "NonAlloc" suffixed versions of 2D physics queries. The "All" ones produce a new C# list containing all the results and leave that to be garbage collected so you cannot reuse the list. You don't want that kind of garbage collection going on. Use the same query names but only use the ones without the suffix so just use "OverlapCircle".

    There are lots of ways to do this but it might be easier for you to just shink the colliders (presumably BoxColliders) on each square. That way, when you do touch them then you know you are significantly overlapped. How much you shrink them can be controlled by you to control your "sensitivity". It's by far the easiest way.
     
  3. polypixeluk

    polypixeluk

    Joined:
    Jul 18, 2020
    Posts:
    53
    Hi Melv.

    Interesting. My current method uses the "All" version because I want an array of colliders to put through a foreach loop so I can compare the collider layers to the layer of the player and adjust the score or life accordingly. If I use the straight OverlapCircle the code fails when I try to assign the query to an array. Is there another way of getting an array of colliders? Edit. I see now I may need to use a list rather than an array? Looking into that now.

    As an update to the rest of my post. I decided the grid of collision detection was silly. Now I'm just using vector3.distance to determine how overlapped the player and the colored square are and using that to scale the effect on the score/life. Much simpler! It works between the player and a single tile but not for multiple tiles for the reason mentioned just above in this post.
     
    Last edited: Aug 3, 2020
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    There's overloads that allows an array or list. The list is better because you don't have to know the size of it therefore ensure the array is at least as big as required. The list will automatically have its capacity increased if required so you'll always get all results so if you reuse it, you'll have minimal garbage produced (only if it's resized but then nothing if it never gets bigger).

    Note that there's Vector2 equivalents when working in 2D. The downside to using Vector3 is that if you have a different Z in two vectors and you do things like distance then the Z difference will be taken into account and this can be hard to notice. Always use Vector2 where you can.
     
    polypixeluk likes this.