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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics2D.OverlapCircle problem

Discussion in '2D' started by noobity, Jul 16, 2015.

  1. noobity

    noobity

    Joined:
    Jun 10, 2015
    Posts:
    8
    Hi guys, I'm working on a 2D game and am trying to work on a problem of moving a 2D object with transform.position. What I'm trying to do is move an object with mouse click and drag, and moving said object by altering its transform. What I want is to make sure that on release, the object isn't lying on another object, so I thought the best way to check would be with Physics2D.OverlapCircle(mouse release point, object radius), but that clearly doesn't work because the said gameObject is in that area. Is there any way to get the function to ignore specific gameObject, or is there some better way? Any help would be appreciated. Thanks!

    Here's a bit of my code for reference:

    Vector3 currPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    Vector2 curr2DPos = newVector2 (currPos.x, currPos.y);

    if (Physics2D.OverlapCircle (curr2DPos, radius)) {
    transform.position = startPos;
    } else ...
     
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    When you're using Physics2D.OverlapCircle, you can also include a layerMask as the third parameter (http://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html). You can use that layerMask to check for overlap with colliders on the layer you want. As long as the object that you're moving isn't on a layer in the layerMask, your script won't be triggered by that GameObject.
     
    Thorlar likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    ArIaNFury008, hghjgfjgf and Thorlar like this.
  4. noobity

    noobity

    Joined:
    Jun 10, 2015
    Posts:
    8
    thanks guys!