Search Unity

Hopefully super easy physics 2d overlapcircle question

Discussion in 'Scripting' started by tomzigza, Sep 19, 2022.

  1. tomzigza

    tomzigza

    Joined:
    Aug 4, 2017
    Posts:
    31
    I have a scene with a bunch of game objects and some of them have circlecollider2ds on them. There are no rigid bodies and I am not doing physics simulation.

    My goal is to use circle overlap as an efficient way to see if anything with a collider is near a given point. I have the following data gained from the debugger:

    //In this case "this" is a gameobject that does have a collider, trying to see if it can see itself
    pos = transform.position (1600.0, 1200.0, 0.0) //What is my position
    transform.GetComponent<CircleCollider2D>().radius 85.33334 //Prove that I do have a collider
    range = 500
    Physics2D.OverlapCircleAll(new Vector3(pos.x, pos.y), range);
    No colliders returned

    Not only can it not see this collider but it cannot find any colliders I have.
    I am not using any layers in the overlap call and my project settings physics 2d layer overlap matrix has everything checked. I have simulation mode on script because I don't want physics to run. I feel like this is something super simple and I am just missing a key point on how this is supposed to work. Thanks for any help anyone provides!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  3. tomzigza

    tomzigza

    Joined:
    Aug 4, 2017
    Posts:
    31
    I can include more code but its not really related to the issue so I was worried it would just confuse.
    Side note I did some raycasting work and that did successfully find the colliders. Only this circle overlap is coming up empty. I also tried adding all layers and a large depth range to try to rule out those but no luck.

    Code (CSharp):
    1. public List<(T t, E e, float dist)> FindEntities<T, E>(FloatPoint pos, float range) where T : EntityUI where E : Entity
    2.   {
    3.     Collider2D[] colliders = Physics2D.OverlapCircleAll(new Vector2(pos.x, pos.y), range, Physics2D.AllLayers,-999,999);
    4.     List<(T t, E e, float dist)> l = new List<(T t, E e, float dist)>();
    5.     Type tType = typeof(T);
    6.     foreach (Collider2D collider in colliders)
    7.     {
    8.       EntityUI eui = collider.gameObject.GetComponent<EntityUI>();
    9.       if (eui.GetType() == tType || eui.GetType().IsSubclassOf(tType))
    10.       {
    11.         Vector2 p = collider.ClosestPoint(pos.ToVector());
    12.         l.Add((eui as T, FindEntity<E>(eui.id) as E, (pos - new FloatPoint(p.x, p.y)).magnitude));
    13.       }
    14.     }
    15.     l.Sort((a, b) => { return a.dist.CompareTo(b.dist); });
    16.     return l;
    17.   }
    As mentioned my scene is some pretty plain gameobjects with a circlecollider, no rigid body. I have tried with trigger checked and not checked.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    How are you concluding that the OverlapCircle is coming up empty? I don't see any log statements in your code and there is a bunch of processing/filtering that is happening that could easily exclude a particular collider from being included in the output of this method.
     
  5. tomzigza

    tomzigza

    Joined:
    Aug 4, 2017
    Posts:
    31
    I debugged it and the array came back size 0. You can see in the original post other debug info I collected to make sure things were setup as I thought they should be.
     
  6. tomzigza

    tomzigza

    Joined:
    Aug 4, 2017
    Posts:
    31
    I found the problem. For future people, if you are not using the physics system to simulate, and just doing collision checks, your colliders will not necessarily be correct and you can call: Physics2D.SyncTransforms(); to force them to update to changes.