Game Development Reference
In-Depth Information
3. Next, we will add the Awake() funcion to set up all the necessary parameters
before calling the Start() funcion by adding the following code:
The Awake() funcion is used to iniialize all the variables before the
game starts or before calling the Start() funcion. You can check out the
following Unity scriping document for more details:
http://unity3d.com/support/documentation/
ScriptReference/MonoBehaviour.Awake.html .
//Set up all parameters before Initialize
public function Awake() : void {
//Get all Transforms of the gameObject include the children and
the transform of this gameObject
waypoints = gameObject.GetComponentsInChildren.<Transform>();
//Set up the length of all transform
int_wayLength = waypoints.Length;
int_wayIndex = 0;
int_nextIndex = 1;
//Checking the orderDirection; if it's false, it means the AI
isn't moving by order, so using the random index of waypoint
if(orderDirection == false) {
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;
}
//Set the direction to zero
v3_direction = Vector3.zero;
//To ignore the first waypoint at the beginning of the game
b_isHitRadius = true;
}
In this funcion, we get the transform of each waypoint in the gameObject and all
the children included in this gameObject by using the gameObject.GetCompon
entsInChildren.<Transform>() to return the array of the Transform type
object. Then, we set the length of this array, the start index, and next index of the
waypoint. We also check to see if orderDirection is false , in which case the
next index waypoint will be orderly picked. On the other hand, if it is true , the next
index waypoint will be randomly picked.
 
Search WWH ::




Custom Search