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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Physics2D.OverlapBox and Physics2D.OverlapCircle keeps returning itself.

Discussion in 'Scripting' started by brockstar512, Jun 13, 2022.

  1. brockstar512

    brockstar512

    Joined:
    May 26, 2020
    Posts:
    3
    I am making a level editor and using the colliders to detect if there is any overlap before the item is place. I even made the added a layer mask as an argument and is returning itself that is not on that layer.

    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         collider = GetComponent<Collider2D>();
    4.  
    5.     }
    Here is the main function
    Code (CSharp):
    1.     public bool CanPlaceObject()
    2.     {
    3.        
    4.         if (collider is BoxCollider2D)
    5.         {
    6.             BoxCollider2D boxCollider = (BoxCollider2D)collider;
    7.             if (Physics2D.OverlapBox(this.transform.position + (Vector3)boxCollider.offset, boxCollider.size, 0,3) != null)
    8.             {
    9.  
    10.                 Debug.Log($"Cannot place box because of {Physics2D.OverlapBox(this.transform.position + (Vector3)boxCollider.offset, boxCollider.size, 0).gameObject.name}");
    11.  
    12.                 return false;
    13.             }
    14.         }
    15.  
    16.         else if (collider is CircleCollider2D)
    17.         {
    18.             CircleCollider2D circleCollider = (CircleCollider2D)collider;
    19.             if (Physics2D.OverlapCircle(transform.position, circleCollider.radius,3) != null)
    20.             {
    21.  
    22.  
    23.                 Debug.Log($"Cannot place circl because of {Physics2D.OverlapCircle(transform.position, circleCollider.radius).gameObject.name}");
    24.                 return false;
    25.             }
    26.  
    27.         }
    28.  
    29.  
    30.         return true;
    31.     }
    Here is where it's being called
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    4.         Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
    5.         transform.position = worldPosition;
    6.  
    7.  
    8.         CanPlaceObject();
    9.     }
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    What do you mean itself? A query doesn't have a notion of self, it's just a query.

    An OverlapBox or OverlapCircle are not associated with a collider. They do exactly what it states in the API reference which is determine what overlaps those shapes.

    If you have a Collider2D and want to check what it overlaps then use: https://docs.unity3d.com/ScriptReference/Collider2D.OverlapCollider.html This call will do what it says but it also exclude the Collider2D its called on because it is related to a specific collider.

    You should NOT modify the Transform when using 2D physics. Modifying the Transform only modifies the Transform, it doesn't update physics there and then. Physics will be forced to update physics components during the next simulation step if you do this (it doesn't happen per-frame unless you ask it to). If you're moving Colliders, you should be moving Rigidbody2D via their API.
     
    PraetorBlue likes this.
  3. brockstar512

    brockstar512

    Joined:
    May 26, 2020
    Posts:
    3
    Thanks for the response. What I mean by self is the logs are saying the overlapping area is the item that I am checking if has anything overlapping with it. Which I suppose makes sense, because I am checking the area based off the area of the collider that I happen to also to be dragging so it will always be overlapping. Noted on the feedback.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You can also use the LayerMask to filter by layer too.