
Unity 2017 Game Optimization
By :

It is safe to say that CPUs are relatively good at multiplying floating-point numbers together, but relatively dreadful at calculating square-roots from them. Every time we ask a Vector3
to calculate a distance with the magnitude
property or with the Distance()
method, we're asking it to perform a square-root calculation (as per Pythagorean theorem), which can cost a lot of CPU overhead compared to many other types of vector math calculations.
However, the Vector3
class also offers a sqrMagnitude
property, which provides the same result as distance, only the value is squared. This means that if we also square the value we wish to compare distance against, then we can perform essentially the same comparison without the cost of an expensive square-root calculation.
For example, consider the following code:
float distance = (transform.position – other.transform.position).Distance(); if (distance < targetDistance) { // do stuff }
This can be replaced...
Change the font size
Change margin width
Change background colour