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 Object closest to the finish line

Discussion in 'Physics' started by MorlockProblems, Dec 16, 2022.

  1. MorlockProblems

    MorlockProblems

    Joined:
    Dec 2, 2021
    Posts:
    4
    I am advancing the objects in my scene with Add force. I focus the camera on the object closest to the finish line. Can you suggest a solution that will cause me fewer problems in terms of performance, where I can calculate the distance of objects from the finish line? Having many objects in my scene creates a problem.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    how many objects you have? and can you check with Profiler, what is the slow part?
    On which device this is running or targeted?

    Or can you show your current script that calculates the distances?
     
  3. MorlockProblems

    MorlockProblems

    Joined:
    Dec 2, 2021
    Posts:
    4
    As the game progresses, the number of objects on the screen can reach 300. My game will be for mobile platforms. I experience the largest performance drop after my object count reaches around 200. Objects that I instantiate are causing this problem. But I also want to get the best performance when calculating the distance.


    Code (CSharp):
    1. void FindClosestBall()
    2.     {
    3.         Ball[] balls= FindObjectsOfType<Ball>();
    4.         Transform closestTarget = null;
    5.         float maxDistance = Mathf.Infinity;
    6.  
    7.         foreach(Ball _ball in balls)
    8.         {
    9.          
    10.          
    11.             Vector3 directionVector = transform.position - _ball.transform.position;
    12.             float targetDistance = directionVector.sqrMagnitude;
    13.  
    14.             if(targetDistance < maxDistance)
    15.             {
    16.                 closestTarget = _ball.transform;
    17.                 maxDistance = targetDistance;
    18.             }
    19.  
    20.         }
    21.         _target = closestTarget;
    22.     }
     
    Last edited: Dec 16, 2022
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    this line is probably the slowest one,
    Code (CSharp):
    1. Ball[] balls= FindObjectsOfType<Ball>();
    so instead of always searching them, try something like:
    - keep list/array/dictionary of spawned objects (when you instantiate, add to list, when you destroy, remove from it)
    - or, if objects are spawned under parent transform, can try looping the children https://docs.unity3d.com/ScriptReference/Transform.GetChild.html

    Even better if objects are moving on a plane, in 1 direction,
    then could just compare X or Y or Z value directly (who is closest has biggest value, without measuring (full 3d) distance)

    ps. can embed code with https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    MorlockProblems likes this.