Narrowing scope of checks

I’d like to reduce amount of some checks based on the distance to player. Since the distance between two points way less complicated, first I check the distance and then do more complicated stuff (like raycasting). Is it worth? What’s the best approach? May be Unity does checks in speed of light?

Don’t worry about it prematurely. If you loop through your objects and do raycasts, it should be pretty easy to later add a distance check into the “if” before the raycast if you run into performance problems. You can use the profiler to see if it helps or not.

As a general rule though, never use absolute distance to check things unless you really need to know the exact distance… Vector3/2. Distance(a, b) and Vector3.magnitude use Pythagoras theorem to get the result, and it contains a slow-ish square root operation that you probably want to avoid.

If you just want to know if something is in/out of range, you only need to compare distances, you don’t need to know the exact distances so you can avoid the square root and compare the squared values:

Vector3 a =;
Vector3 b =;

Vector3 fromTo = b - a;
const float range = 5;

if (fromTo.magnitude < range ) // slower
if (Vector3.Distance(a, b) < range ) // slower

const float rangeSquared = 5*5;
if (fromTo.sqrMagnitude < rangeSquared) // faster