Graphics Reference
In-Depth Information
AIAttackState.look_and_destroy will use raycasting to look out forward from this
enemy's transform to see whether a potential target is in front. tagOfTargetsToShootAt
is set to hold a tag applied to enemies (via the Unity editor Inspector window Tag drop-
down), and Transform.CompareTag() is used to make sure that an object hit by a raycast
has the correct tag in place:
if( currentState==AIAttackState.look_and_destroy )
{
if(Physics.Raycast( myTransform.position,
myTransform.forward, out rayHit ))
{
// is it an opponent to be shot at?
if( rayHit.transform.CompareTag
( tagOfTargetsToShootAt ) )
{
When an object is found in front of this transform with the correct tag applied, doFire
is set to true, and firing can happen later in the function:
doFire=true;
}
}
} else {
The else condition is provided as a catch-all. If in doubt, fire it out! If AIAttackState
makes no sense, the function will just keep firing:
// if we're not set to random fire or look and
// destroy, just fire whenever we can
doFire=true;
}
}
The state of doFire is a condition for executing the next chunk of code:
if( doFire )
{
The Boolean variable onlyFireWhenOnScreen suggests whether or not this enemy
should be allowed to fire when not being drawn by the camera. The renderer component
referenced by rendererToTestAgainst is checked using the Renderer.IsVisibleFrom() func-
tion provided by Unity. IsVisibleFrom() takes a camera as a parameter and will return true
if the renderer is visible from that camera (and, of course, false when it is not).
The code sets doFire to false when onlyFireWhenOnScreen is true and the renderer is
not visible on screen. If firing is cancelled in this way, it also drops out with a return state-
ment since there is no value left in executing the script further on this pass:
if(onlyFireWhenOnscreen && !rendererToTestAgainst.
IsVisibleFrom( Camera.mainCamera ))
{
doFire=false;
return;
}
Search WWH ::




Custom Search