Graphics Reference
In-Depth Information
// automatically change the value of moveDirection
// before we do the actual move
if( obstacleFinderResult==1 ){ // GO LEFT
SetAIState( AIState.stopped_turning_left );
}
if( obstacleFinderResult==2 ){ // GO RIGHT
SetAIState( AIState.stopped_turning_right );
}
When obstacles are detected on both the left and right sides of the bot, the return result
from IsObstacleAhead() is 3. Instead of trying to turn to avoid whatever is blocking the
way forward, the bot goes into a reverse state—AIState.backing_up_looking_for_target:
if( obstacleFinderResult==3 ){ // BACK UP
SetAIState( AIState.backing_up_looking_for_
target );
}
Now that the bot has finished deciding whether or not to turn or back up to avoid
obstacles, all that is left for it to do is move forward. The MoveForward() function is called
as we break for the next case:
// all clear! head forward
MoveForward();
break;
The next state is AIState.chasing_target:
case AIState.chasing_target:
// chasing in case mode, we point toward the target
// and go right at it!
As this mode's main objective is to chase the transform in followTarget, we need to
first make sure that there is an actual transform in there to follow. If followTarget is null,
the script changes the state back to AIState.moving_looking_for_target:
// quick check to make sure that we have a target
// (if not, we drop back to patrol mode)
if( followTarget==null )
SetAIState( AIState.moving_looking_for_
target );
TurnTowardTarget() will rotate the transform toward the target transform passed
into the function as a parameter, at the speed set by the float variable modelRotateSpeed.
Note that the transform to be rotated will be the transform held by the variable rotate-
Transform, as described in the Init() function earlier in this section, not specifically the
transform to which the script is attached:
// the TurnTowardTarget function does just that, so
// to chase we just throw it the current target
TurnTowardTarget( followTarget );
Unity's Vector3.Distance function is used to check that the position of the transform
in followTarget is not too close or too far away:
Search WWH ::




Custom Search