Vector3.Distance impacts your Game Performance in Unity, Destroy it

There are a bunch of Methods to Save Performance in Unity. One of them is not to use Vector3.Distance since its  just a Wrapper around a very smple Function.

Don't use Vector2.Distance or Vector3.Distance

Yes, do not use it. There are better ways to do the distance check.

Just subtract the 2 vectors and get the sqrMagnitude value, which is the same as you would do a Vector2/3.Distance check.

Vector2.Distance or Vector3.Distance is a wrapper and inside the function nothing happens other than positionOne – positionTwo + Square calculation and in return you get the distance, in a more complicated way.

We can take a look inside the wrappers and see how they work.

					        //We wanna Calculate the Distance between two Vectors
        Vector3 positionOne = Vector3.positiveInfinity;
        Vector3 positionTwo = Vector3.negativeInfinity;

        //The Easiest Way is to use Vector2/3.Distance to Check the Distance
        //But it takes much more Performance and is basicially the Same Calculation
        float distance = Vector3.Distance(positionOne, positionTwo);      

        //as Calculating the sqrMagnitude or magnitude,
        //both will give you the Distance as Result
        float distanceSqrMag = (positionOne - positionTwo).sqrMagnitude;
        float distanceMag = (positionOne - positionTwo).magnitude;

					// Distance Calculation of Vector3
public static float Distance(Vector3 a, Vector3 b)
    Vector3 vector = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
					// Distance Calculation with Magnitude
public static float Magnitude(Vector3 a)
    return Mathf.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
					// Mathf.Sqrt
public static float Sqrt(Vector3 a)
    return (a.x * a.x + a.y * a.y + a.z * a.z);
					// Distance Calculation with SqrMagnitude
public static float SqrMagnitude(Vector3 a)
    return a.x * a.x + a.y * a.y + a.z * a.z;

Use sqrMagnitude for Distance Check

As you can see from the different functions, they all perform the same calculations. The fastest is usually SqrMagnitude.

The slowest is obviously the Vector3 distance check. The system even creates a local variable to store the vector information by reference and then passes it to the Mathf.Sqrt function. This causes unnecessary CPU power and garbage to be handled and collected. If done quickly and often, this can have a big impact on game performance.

SqrMagnitude is exactly what we need to pass the wrapper functions. It is the fastest approach to calculate a distance between two vectors.

In the end, it depends on your project which approach you want to use. Just try it out and check the performance yourself. Vector3.Distance is only used to make the code easy to read, so that others involved in the project know exactly what is happening.

But you can mention in your project’s documentation that you are using magnitude as a distance check instead of Vector3 or magnitude, so that everyone knows about it before diving into the project.

There are more approaches, ideas, and things you can do to improve game performance in a Unity project. Check out our other articles.

Leave a Reply

Your email address will not be published.