unity3d Optimization Fast and Efficient Checks


Avoid unnecessary operations and method calls wherever you can, especially in a method which is called many times a second, like Update.

Distance/Range Checks

Use sqrMagnitude instead of magnitude when comparing distances. This avoids unnecessary sqrt operations. Note that when using sqrMagnitude, the right hand side must also be squared.

if ((target.position - transform.position).sqrMagnitude < minDistance * minDistance))

Bounds Checks

Object intersections can be crudely checked by checking whether their Collider/Renderer bounds intersect. The Bounds structure also has a handy Intersects method which helps determine whether two bounds intersect.

Bounds also help us to calculate a close approximate of the actual (surface to surface) distance between objects (see Bounds.SqrDistance).


Bounds checking works really well for convex objects, but bounds checks on concave objects may lead to much higher inaccuracies depending on the shape of the object.

Using Mesh.bounds is not recommended as it returns local space bounds. Use MeshRenderer.bounds instead.