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

Need help understanding Physics2D.OverlapArea()

Discussion in '2D' started by Ashky, Apr 25, 2015.

  1. Ashky

    Ashky

    Joined:
    Dec 16, 2013
    Posts:
    3
    Hello everyone!

    I'm trying to write a script that instantiates a square prefab at a random location on screen. If the space is already occupied by a previously instantiated square, it should just write something in the console. Here's the code I've written:
    Code (CSharp):
    1.     public void SpawnSquares(int difficulty){
    2.         float R1=Random.Range(25.0f,_screenWidth);
    3.         float R2=Random.Range(25.0f,_screenHeight);
    4.         _overlapA=new Vector2(R1-50f,R2+50f);
    5.         _overlapB=new Vector2(R1+50f,R2-50f);
    6.         if(Physics2D.OverlapArea(_overlapA,_overlapB,layermask,Mathf.Infinity,Mathf.Infinity)==null){
    7.             _position=Camera.main.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(R1,R2,10));
    8.             Instantiate(Resources.Load("Square",typeof (GameObject)),_position,Quaternion.identity);
    9.  
    10.         }
    11.         else{
    12.             Debug.Log("avoided collision");
    13.         }
    14.     }
    The layermask variable is set layermask=1<<9; because all the squares are located on layer 9.

    What I expected this to do was generate a random location and modify _overlapA and _overlapB to account for the size of the squares, then check if there are any colliders in that certain area. If there are not (OverlapArea==null) then it should spawn a new square at that position. If the space is occupied just write to console.

    What is actually happening? The execution never branches off on else, so that means Physics2D.OverlapArea(_overlapA,_overlapB,layermask,Mathf.Infinity,Mathf.Infinity) always returns null. However, after calling SpawnSquares() around 10 times the screen is pretty much filled up with squares so it should at least detect one collider.

    What am I doing wrong? My project has stalled for the last two days because I can't seem to solve this issue, so any help would be greatly appreciated.

    Thanks for your time!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    You've got both the min and max Z position set to infinity which is wrong. Just don't specify them if you're not using them as they are optional args. Note the minDepth default is (minus) -Mathf.Infinity and the maxDepth default is Mathf.Infinity.

    Also, in-case your layer is wrong, again, just omit it and it'll select all layers.

    These are basic debugging things to try if in doubt.
     
  3. Ashky

    Ashky

    Joined:
    Dec 16, 2013
    Posts:
    3
    I've modified the code accordingly, but the behaviour remains the same. I've removed all 3 of those parameters, and out of 100 calls, only 2 calls wrote to the console, which is very bad. After 20 calls it should pretty much write the console all the time.

    Does this maybe have something to do with local coordinates and world coordinates? I don't understand them enough to be able to tell what would the problem be, but I feel that checking with a vector2 position and instantiating with ScreenToWorldPoint could be an issue. Maybe OverlapArea doesn't detect anything because it's using world coordinates. Thoughts?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Physics doesn't operate in screen coordinates, that wouldn't make sense at all as you can have physics located off-screen.

    All the Physics2D Overlap and Cast methods use world coordinates, they have to as they're obviously not local (relative) to anything as they don't refer to a particular Rigidbody2D.

    That'll be your problem. :)