Graphics Reference
In-Depth Information
as the player. When a car is set to use AI, input is used from the AI script rather than from
an input controller.
The AI script uses horz and vert (both floats) to store horizontal and vertical input,
which are the standard variable names used in other scripts in this topic:
// reset our inputs
horz=0;
vert=0;
Later in this section, we will look at the IsObstacleAhead() function. It uses raycasting
to check for any obstacles in front of the bot and returns a number back:
0
No obstacles were found.
1
An obstacle was detected front, left side only.
2
An obstacle was detected front, right side only.
3
Obstacle or obstacles detected on both sides.
On the next line of code, the IsObstacleAhead() result is placed into the temporary
variable obstacleFinderResult (an integer) for use later in the UpdateAI() function:
int obstacleFinderResult= IsObstacleAhead();
Next in the script, a switch statement determines exactly what the AI should be doing
based on the state held in the variable currentAIState (which is of type AIState):
switch( currentAIState )
{
// -----------------------------
The first case is AIState.moving_looking_for_target, which tells the bot to move for-
ward through the level until it finds the target specified in the variable followTarget:
case AIState.moving_looking_for_target:
// look for chase target
if( followTarget!=null )
LookAroundFor() is a function we will look at in detail later in this section. For
now, let's us just say that it looks for the target transform in followTarget. The function
has no return value and will change the AIState directly, if the target transform should
be chased:
LookAroundFor( followTarget );
At the start of the UpdateAI() function, the call to IsObstacleAhead() was made
and its return value placed into obstacleFinderResult. The next lines react to that result,
changing the AIState to turn left or right depending on whether obstacles have been
detected:
// the AvoidWalls function looks to see if there's
// anything in front. If there is, it will
Search WWH ::




Custom Search