Game Development Reference
In-Depth Information
Then, we added two funcions: the irst one is the
GetDirectionToPlayer()
funcion which was used to make the enemy move towards the player, and the
AwayFromWaypoint()
funcion was used to check the distance between the enemy and
the next waypoint while the enemy is chasing the player. If it does, then we will return
true
and make our enemy go back to the next waypoint.
The
GetDirectionToPlayer()
funcion will make the enemy follow the player at a
certain distance, and the
AwayFromWaypoint()
funcion will make the enemy go back to
the waypoint if its posiion is too far away.
Next, we used the
OnDrawGizmos()
funcion to create the visual for the waypoint game
objects to show in the editor. The
waypoint
is the empty game object, which is someimes
difficult to edit in the editor because we cannot see it in the editors.
It is beter to use gizmo than trying to use camera layers
and meshes for the waypoint.
So, using gizmo is the best soluion and the beter way that Unity provides us to see the
visual of some empty game object. We also have the
trigger
parameter in the
Inspector
view to tell our enemy to walk randomly or by the order of the waypoint index.
Classified Intel
At the beginning of the script, we use
GetComponentsInChildren.<Transform>()
funcion. Have a look at the Unity document at the following link:
http://unity3d.com/support/documentation/ScriptReference/GameObject.
GetComponentsInChildren.html
.
You might ask—why do we call it something different than the document, and why don't we
just use the
GetComponentsInChildren(Transform)
or the difference between those
two funcion calls? Well, as is speciied in the document, we will see that the
GetCompon
entsInChildren(Transform)
funcion will return
Component[]
Or, it means that the
script is atached to this game object but not the
Transform[]
type, which we need for our
Waypoints
script.