Game Development Reference
In-Depth Information
4. Next, we add the funcion to set up the posiion of the enemy at the start posiion of
the waypoint, which we will call from the Start() funcion of our AIController
script in the next step. Add the following code:
public function StartPosition() : Vector3 {
return waypoints[0].position;
}
This way, we can make sure that the enemy will always start at the waypoint index 0 .
5. Then, we will create the core of this funcion, which will calculate and return the
direcion of the current posiion character to the waypoint posiion. Type the
following code:
//Return the direction of the enemy toward the next waypoint
public function GetDirection( _AI : Transform ) : Vector3 {
if (Vector3.Distance(_AI.position, waypoints[int_nextIndex].
position) <= radius) {
//Only check once when the AI hit the way point
if (!b_isHitRadius) {
b_isHitRadius = true;
//Update the current way index
int_wayIndex = int_nextIndex;
//Get Direction by order
if (orderDirection == true) {
//Get the next way index
int_nextIndex = (int_nextIndex + 1) % int_wayLength;
} else {
var int_randomWay : int = Mathf.Floor(Random.value *
int_wayLength);
//Checking to make sure that the waypoint length is more
than 1
if (int_wayLength > 1) {
//Use Random Index
while (int_wayIndex == int_randomWay) {
int_randomWay = Mathf.Floor(Random.value * int_
wayLength);
}
}
int_nextIndex = int_randomWay;
}
}
} else {
b_isHitRadius = false;
}
 
Search WWH ::




Custom Search