Graphics Reference
In-Depth Information
Since most of the core logic takes place in the Update() function, which is automati-
cally called by the Unity engine at each frame, it is declared here as a virtual function that
will almost certainly be overridden by any class deriving from the BaseArmedEnemy class:
public virtual void Update ()
{
If canControl is false, no weapon control is allowed so this function drops out early:
// if we are not allowed to control the weapon, we drop out
// here
if(!canControl)
return;
It may seem redundant having a canControl variable that will drop out of this func-
tion along with a variable to decide whether or not to actually execute the firing code, but
the reason is consistency; all of the player control scripts use canControl, and it is used
here too. Although this script in its current form only deals with weapon control, there is
nothing to stop it growing into something more complex later on. To make sure that the
script remains extensible for potential future use, the logic to fire is dependent on this-
GameObjectShouldFire being set to true:
if(thisGameObjectShouldFire)
{
Depending on the AIAttackState this enemy is currently set to use, it may be required
to be able to have a line of sight to a potential target before firing. doFire is a Boolean vari-
able used to tell whether or not the conditions to fire (such as line of sight) have been satis-
fied and firing is allowed. Before fire condition checking starts, the assumption is made
that firing will not be allowed, and doFire is set to false:
// we use doFire to determine whether or not to fire
// right now
doFire=false;
There will always be a pause in-between firing. The Boolean variable canFire is used to
determine whether or not the script should be allowed to fire at this time.
if( canFire )
{
AIAttackState.random_fire means just that—there is no checking other than to gen-
erate a random number and see whether it turns out to be over a set number (98, in this
case). When the randomly generated number is high enough, doFire is set to true so that
firing happens:
if( currentState==AIAttackState.random_fire )
{
// if the random number is over x, fire
if( Random.Range(0,100)>98 )
{
doFire=true;
}
} else
Search WWH ::




Custom Search