Game Development Reference
In-Depth Information
16 {
17 //Check if we can see player
18 if(CanSeePlayer)
19 {
20 // can see player?, chase to attack
21 StartCoroutine(State_Chase());
22 yield break;
23 }
24
25 //Wait for next frame
26 yield return null;
27 }
28 }
29 //--------------------------------------------------
The following are the comments for code sample 7-3:
Line 03 : State_Idle is coded as a Coroutine. For more information on
Coroutines, see the online Unity documentation at http://docs.unity3d.
com/Manual/Coroutines.html . In short, a Coroutine works like an
asynchronous function (as a code block that runs in the background, parallel
to other functions). For this reason, the infinite loop in line 15 will not cause
a crash because a Coroutine runs like a separate thread. Coroutines always
return type IEnumerator and always feature a yield statement somewhere
within their body.
Line 09 : The animator SetTrigger function is called in this line; it passes
the hash code for the string Idle as an argument to set the Idle trigger in
the Mecanim graph, initiating a playback of the idle animation. This links
the C# FSM to the Mecanim FSM. Notice that in line 12, the Stop function
is called for the NavMeshAgent component to stop any movement that the
object might have been performing. This is because while the idle animation
is playing, the enemy should not be moving.
Line 15 : Here, the State_Idle function enters an infinite loop; that is, it'll
loop frame by frame as long as the enemy is in an Idle state. While the Idle
state is active, everything within the loop executes every frame that allows
the object to update and change its behavior over time.
Line 18 : One exit condition for the Idle state, other than waiting for the
idle animation to complete, is if the player is seen in the interim. Player
visibility is determined by the Boolean variable CanSeePlayer (the details
of line of sight are considered later). If CanSeePlayer is true , the Chase
state is activated using the StartCoroutine function, and the Idle state is
terminated with a call to yield break.
 
Search WWH ::




Custom Search