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

Question Overlap Circle : optimization issues

Discussion in 'Scripting' started by HotChiliGames, Apr 19, 2023.

  1. HotChiliGames

    HotChiliGames

    Joined:
    Sep 28, 2022
    Posts:
    3
    Hi,

    I'm a programmer in an indie studio and I ran into optimization issues:

    Optimization trouble with the use of “Physics2D.OverlapCircleNonAlloc”.
    As you can see in the profiler it uses more resources (Time ms) than I believe it should.
    Here is a snippet of the script that I run into Update function :
    Code (CSharp):
    1. m_hitCount = Physics2D.OverlapCircleNonAlloc(transform.position, m_distance, hits, m_mask);
    Do you have an idea where the problem could come from? Thanks for any help you could bring!
     

    Attached Files:

  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    How large is the circle radius, and how many objects usually overlap it?

    If you're checking for a large circle and many objects fall within it, that's probably going to be costlier than checking a small circle overlapping just a few colliders.
     
    Bunny83 and MelvMay like this.
  3. HotChiliGames

    HotChiliGames

    Joined:
    Sep 28, 2022
    Posts:
    3
    The size of the circle radius is 30 and usually it overlaps less than 10 objects that have the corresponding layer.
    I think I found the source of the problem, I had a poligon collider with over 500 points. I disabled it and it fixed the optimization problem.

    Thank's for your help!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Still, that sounds like a lot of time spent. The main thing to look for is the number of shapes a collider produces. In the case of a PolygonCollider2D, the number of points isn't relevant, it's the number of shapes (polygons) it produces. You can see this count on any 2D Collider in the inspector (in the "Info" foldout). Each one of those shapes is discoverable.

    Another thing to note is to try to not use any of the NonAlloc are they'll be deprecated in upcoming versions of Unity. All querys have non-allocating versions, just remove the "NonAlloc" suffix i.e. "Physics2D.OverlapCircle" which allows you to pass in an array or better still, a List<T> for the results so you don't have to have a larger than needed array.