Game Development Reference
In-Depth Information
The equation of myVector.sqrMagnitude is as follows:
float sqrMagnitude = myVector.x* myVector.x+ myVector.y*
myVector.y+ myVector.z* myVector.z;
As we can see, the difference is that sqrMagnitude doesn't need to calculate
Math.Sqrt or the square root, which makes it faster to calculate.
So, if we want to compare the distance, sqrMagnitude is often the best choice because
using the distance's power of 2 is a lot faster than the square root of the magnitude, as we
can see in the following script:
If (myVector3.sqrMagnitude < distance*distance) { … }
Note
For more details, visit the following websites:
http://docs.unity3d.com/Documentation/ScriptReference/Vector3-magnitude.html
http://answers.unity3d.com/questions/384932/best-way-to-find-distance.html
Next, we added the GetDirectionToPlayer() function to find the direction
between our enemy and the player by using the y position of the waypoint to make sure
that the direction gets calculated on the XZ plane. Then, we add the GetDirection()
function, which checks the distance between the enemy position and waypoint position, as
shown in the following diagram:
Search WWH ::




Custom Search