Game Development Reference
In-Depth Information
Objective complete - mini debriefing
What we have done here is created the waypoint system that basically controls the move-
ment of the enemy. We started by creating the Waypoint script, which gets the showPath
and radius variable from the WaypointsContainer script. Then, we used those variables
to show the gizmo objects, which are the icons and wire spheres in the OnDrawGiz-
mos() function.
Next, we created the WaypointsContainer script that has all the necessary code to
control the enemy movement. First, we have the AwayFromWayPoint() function that
will check the distance between the enemy's current position to the next waypoint. In this
function, we've used myVector3 . sqrMagnitude to check for the distance. If we take a
look at the Unity documentation, we can also use Vector3.Distance() or myVect-
or3.magnitude to check for the distance between two positions. So, why did we use
sqrMagnitude instead of others?
Let's take a close look at the equation of the Vector3.Distance() function:
Vector3 vector = new Vector3 (a.x - b.x, a.y - b.y, a.z -
b.z);
float distance = Math.Sqrt(vector.x* vector.x+ vector.y*
vector.y+ vector.z* vector.z);
As we can see, we need to find the difference between two vectors first, use the power of 2
to the result vector, and square root it.
Note
The difference between two vectors is the vector from the tail of the base vector to the head
of the reverse of another vector. Then, the root of the square is just to calculate the mag-
nitude of this vector.
Assuming myVector3 is the difference between vectors, a and b like the vector para-
meter in the preceding script, then the following is the equation of myVect-
or.magnitude :
float magnitude = Mathf.Sqrt(myVector.x* myVector.x+
myVector.y* myVector.y+ myVector.z* myVector.z);
Search WWH ::




Custom Search