Graphics Reference
In-Depth Information
Here, if the variable foundTarget is false, we know that no target was found by the
previous distance checks. If a target has not been found, the current chase target needs to
be cleared out and the AI controller state switched back to its moving_looking_for_target
stage:
if( foundTarget==false )
{
// clear target
AIControlComponent.SetChaseTarget( null );
With the chase target set to null, we set the AIState to AIState.moving_looking_for_
target to make sure that the AI will be moving even though the target has been set to null:
// change AI state
AIControlComponent.SetAIState( AIState.moving_looking_
for_target );
}
}
CanSee() is almost the exact same code as the CanSee() function built into the
BaseAIController.cs script. Take a look at the breakdown in Chapter 9 for a full descrip-
tion on how this function works:
public bool CanSee( Transform aTarget )
{
// first, let's get a vector to use for raycasting by
// subtracting the target position from our AI position
tempDirVec= Vector3.Normalize( aTarget.position -
myTransform.position );
// let's have a debug line to check the distance between the
// two manually, in case you run into trouble!
Debug.DrawLine( myTransform.position, aTarget.position );
// cast a ray from our AI, out toward the target passed in
// (use the tempDirVec magnitude as the distance to cast)
if( Physics.Raycast( myTransform.position +
( visionHeightOffset * myTransform.up ), tempDirVec, out hit,
chaseDistance ))
{
// check to see if we hit the target
if( hit.transform.gameObject == aTarget.gameObject )
{
return true;
}
}
// nothing found, so return false
return false;
}
}
Search WWH ::




Custom Search