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

Optimize item detection and passthrough

Discussion in 'Scripting' started by a7777777777, May 7, 2020.

  1. a7777777777

    a7777777777

    Joined:
    Apr 23, 2020
    Posts:
    6
    I will have an object, which will need to detect GameObjects will particular class and return them for processing. This is a demo script that I've crafted:
    Code (CSharp):
    1. public class PerformanceTest : MonoBehaviour
    2. {
    3.  
    4.    class Player { }
    5.  
    6.  
    7.     void Start()
    8.     {
    9.        // Say, there's 10000 objects
    10.       var playersFound = CheckIfPlayerNearby(Vector3.zero, 500.0f);
    11.  
    12.       // foreach() {} and conitnued processing
    13.     }
    14.  
    15.  
    16.     List<Player> CheckIfPlayerNearby(Vector3 center, float radius)
    17.     {
    18.        Collider[] hitColliders = Physics.OverlapSphere(center, radius);
    19.        List<Player> output = new List<Player>();
    20.    
    21.        foreach (var hitCollider in hitColliders)
    22.        {
    23.           Player pc = hitCollider.GetComponent<Player>();
    24.           if (pc != null) output.Add(pc);
    25.        }
    26.  
    27.        return output;
    28.     }
    29.  
    30. }
    31.  
    Here are my questions:
    1. Is there a way to optimize script above? I couldn't forgive myself unnecessary 10 frames or 50ms CPU lost due to lack of optimization on this one. Especially when this will become a highly reused code. I know I could decrease the radius of detection but the point is optimization of script, not game hedging.
    2. How do I use
    OverlapSphereNonAlloc
    . My IDE screams that I should use it instead, I tried for a second, and I adjusted the parameters but I somehow can't get it done.

    Edit: I've just found out that I can use layerMask to specify the layer. Do I specify the layers that I want it to check, or do I specify the layers I want it to ignore? If I want to use multiple layers, do I simply use
    |
    ?
    Edit 2: I don't quite care about precision, for all I care the actual shape of said sphere could be icosahedron.
     
    Last edited: May 7, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It will be much simpler and more performant to just use a Sphere Collider set as a trigger collider with the appropriate layer collisions set up in physics settings and just use OnTriggerEnter and OnTriggerExit to detect the player entering or leaving.
     
  3. a7777777777

    a7777777777

    Joined:
    Apr 23, 2020
    Posts:
    6
    I got that, but I can't see a way to tell OnTriggerEnter to focus on a single layer. I don't want to mix up Physics settings either because this won't be the only physics operation executed in the game, other physics interactions must remained untouched. Or do I have to do a tag comparison manually?
     
    Last edited: May 8, 2020
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    They should really add an example to the docs. Here's an explanation.
     
    a7777777777 likes this.