Game Development Reference
In-Depth Information
if(attempts >= 25)
{
Debug.LogWarning("LookBusy.cs:FindTarget()
--> Could not find a target farther than the
mininum distance. Either lower the mininum
distance or space the targets farther apart.");
yield return NodeResult.Failure;
}
}
yield return NodeResult.Success;
}
In this script, we first check whether we have at least two targets in the game tagged
to select from, and if we don't have them, the script reports an error. You'll notice that
the error doesn't break the game, it just gives a specific warning on the log of what
you need and where the log was posted from. Next, the script selects a random pos-
ition from the list of nodes, and if the position within a character's minimum distance
(and not the same target the character is already on), the script returns the position.
This random position is chosen from the list of nodes no more than a constant num-
ber of times, that is, 25 times. This random choosing method doesn't guarantee suc-
cess, but it is a quick and easy way to choose a random target.
Besides picking a random place for a character to walk to, we also need a random
amount of time for the NPC to stay at the location they go to. The HangAround
method does this:
public Action HangAround()
{
// Choose a random time to wait
float randomtime =
Random.Range(this.ShortWaitTime,
this.LongWaitTime);
while(totalTime < randomtime)
{
totalTime += UnityEngine.Time.deltaTime;
yield return NodeResult.Continue;
}
Search WWH ::




Custom Search