Game Development Reference
In-Depth Information
9. The core logic for this class happens in the
Update()
method each time Un-
ity invokes this function and we track total elapsed time in
t
as shown in the
following line of code:
t += time.deltaTime;
10. Once the final state is completed, we tell each NPC to change state to
patrol
. This has the effect of making them start to follow their spline paths
via their component
SplineMgrs
as shown in the following code snippet:
if (currentState == numStates + 1)
{
ncpA.GetComponent<npcScript>().SetState(npcScript.npcState.patrol);
npcB.GetComponent<npcScript>().SetState(npcScript.npcState.patrol);
11. If the elapsed time exceeds the state time, we reset the
stageTime
to zero
plus any fractional difference beyond the
stageTime
that has been incurred
and then increase the stage count by
1
. This is slightly more accurate than
simply setting
t
to
0
for each stage and is prone to fewer errors over many
stages. This can be accomplished with the following code snippet:
if (t > stageTime)
{
currentState++;
t -= stageTime;
}
Congratulations! Now, the
raceStartup
Prefab will activate the NPCs after an appro-
priate time has passed. Conveniently, this delay is adjustable inside the Unity Editor.
This is a good thing as it lets the player to get a small head start on the other racers.