So currently ive been using the physics.castSphere method with a small radius to find the closest of an object with a tag.

However this is pretty performance heavy so my question is, is there a way to optimize it?

i have also been looking at keeping a list of gameobjects and just looping through those however doing that a few times a frame for each object searching also seems rather performance intensive.

Here is my case:

50 individual characters on team 1

50 individual characters on team 2

each character has to find the closest of the character from the other team.

What is the fastest and least performance heavy way of doing it?

  • \$\begingroup\$ Have you actually profiled? Looping over 100 objects a few times is nothing \$\endgroup\$ Aug 28, 2019 at 12:45
  • \$\begingroup\$ @ratchetfreak so you are suggesting that i keep going with the "keep a list in memory" approach? This approach can get messy when objects are spawned / despawned \$\endgroup\$ Aug 28, 2019 at 12:46
  • \$\begingroup\$ @ratchetfreak however it might be the fastest way \$\endgroup\$ Aug 28, 2019 at 12:47
  • \$\begingroup\$ There's no method called physics.castSphere — do you mean Physics.SphereCast, which fires a sphere along a ray (significantly heavier than you want) or Physics.OverlapSphere, which finds objects in a spherical radius (and has a non-allocating version you can use)? Do you only care about the closest object within a particular "awareness" radius, or do you still need the closest object even when it is very far away? \$\endgroup\$
    – DMGregory
    Aug 28, 2019 at 13:05

1 Answer 1


if calculations are heavy you can divide them using something like Coroutine to avoid lags and...

if count of object that you need to calculate their distance is low you can simply calculate their distance or use rayCast if its important that nothing is between them.

if they are too many you can put a circle or... collider around it that only calculate those are in the range. you can make trigger bigger if there is no object in range.

if you use trigger you have to use layer masks to only calculate range for those only important.

  • \$\begingroup\$ It's worth noting that Coroutines are not meant to be used to avoid lag on something that is meant to run every frame. Coroutines are meant to run code without blocking the main thread. Depending on what one is working on, they can also make it harder to access objects. \$\endgroup\$ Aug 28, 2019 at 13:16
  • \$\begingroup\$ @TomTsagk as i know unity has not multiple thread everything is in one thread. coroutines only calculate frame timing to do jobs in some amount of time. sometimes in your games you need to use for loops. its a timing process. coroutine is the main solution for loops and ... on game main Update thread \$\endgroup\$
    – virtouso
    Aug 28, 2019 at 13:25
  • \$\begingroup\$ Yes but if you want to run something on every frame, and that something takes a long time to compute, I don't think Coroutines will improve that at all. My point was, if someone is concerned about improving performance, Coroutines are not the right way to go. \$\endgroup\$ Aug 28, 2019 at 13:29
  • \$\begingroup\$ @TomTsagk calculating distance in every frame is a waste. certainly. even if it always is being done, it should be done in every 4 or 5 seconds not in every frame. \$\endgroup\$
    – virtouso
    Aug 28, 2019 at 13:44
  • \$\begingroup\$ I really disagree with that. Can you image a particle system where the particles can only collide with each other every 4 or 5 seconds? Plus this is getting a little off-topic, I just wanted to point out that Coroutines are not some special functions that are immune to performance issues. \$\endgroup\$ Aug 28, 2019 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .