Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Selecting every Collider2D in a Scene (codewise)

Discussion in '2D' started by Fe0dor, Feb 5, 2020.

  1. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    Hello,

    I am working on a directional teleporting feature and am currently stuck on a problem.
    Because I am working on a grid I had the Idea of a Vector3 checking every tile until it hits a wall.
    I wrote a method that actually worked but only if I specify the Collider the the Method is looking for.
    So now I am stuck and maybe there is a way to combine every Collider2D on the Scene into one big Collider.
    I know there are Arrays that I could use to handle that but no matter how hard I tried it ended up not working.
    It would be very nice to tell me other possibilitys ore just suggest things that might work.

    Here is the Code that actually worked (for one Collider)

    Code (CSharp):
    1.  
    2. //Returns the amount of steps the Player has to do to reach the next wall
    3.     float stepsUntilColliderR()
    4.     {
    5.         int i = 0;
    6.         Vector3 checkPoint = gameObject.transform.position;
    7. //IsInside() returns a bool that is true, if given Vector3 is inside given Collider
    8.         while(!IsInside(wallCollider,checkPoint))
    9.         {
    10.             checkPoint = new Vector3(checkPoint.x + 1, checkPoint.y);
    11.             i++;
    12.         }
    13.      
    14.         return i-1;
    15.     }
    16.  
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    what are you doing in your IsInside function? You can do raycasts or collision checks against an entire object layer rather than a single collider.
     
    Fe0dor likes this.
  3. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    Code (CSharp):
    1.  public static bool IsInside(Collider2D c, Vector3 point)
    2.     {
    3.         Vector3 closest = c.ClosestPoint(point);
    4.        
    5.         return closest == point;
    6.     }
    7.  

    A raycast would not be optimal because I have to count steps and just want to check if a certain point is inside an obsticle. I found a solution with Arrays but I hav no clue how or why it works. Checking Collision with certain layers sounds interesting tho.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    What you're doing looks very expensive.

    There are plenty of 2D physics queries that will allow you to do this. Certainly one you can use which doesn't require you knowing what's at a position is Physics2D.OverlapPoint but there's overlap circle, box, collider etc too. You can filter what you're checking for too as that's a basic function of the physics system so you (for instance) only include "Wall" layers.

    I'm sure you're aware of this because they're all there listed in a single page in the API docs so I'm wondering why you're doing it the way you are. I'm not sure why you're doing only a point either unless you can guarantee that a collider will be overlapping that point if you can't move to it so maybe an OverlapCircle or OverlapBox would be better that's slightly smaller than your grid size.
     
    Last edited: Feb 5, 2020
    Fe0dor and LiterallyJeff like this.