Graphics Reference
In-Depth Information
Like its real-life counterpart, our cars use a steering system to move them left or
right. Rather than force the car toward a particular direction, the AIController will sup-
ply inputs that our CarController_MVD script can use as though they were user inputs.
As you may recall from Chapter 9, the AI system has a specific state for providing steer-
ing inputs based on waypoints. Here the AIController object is called to set its state to
AIStates.AIState.steer_to_waypoint:
// tell the AI controller to go into waypoint steering
// mode
AIController.SetAIState( AIStates.AIState.steer_to_waypoint );
As this is an AI controller vehicle, the default input method needs to be disabled (oth-
erwise, the AI may have to fight along with the player input!):
// disable our default input method
default_input.enabled=false;
The BaseArmedEnemy script from Chapter 9, Section 9.3 is used as a weapon control-
ler. If one is not already attached to the player, one will be added:
// add an AI weapon controller
gunControl= myGO.GetComponent<BaseArmedEnemy>();
// if we don't already have a gun controller, let's add one
// to stop things breaking but warn about it so that it may
// be fixed
if( gunControl==null )
{
gunControl= myGO.AddComponent<BaseArmedEnemy>();
Debug.LogWarning("WARNING! Trying to initialize car
without a BaseArmedEnemy component attached. Player
cannot fire!");
}
The gun controller needs to shoot whenever it sees an enemy in front of it. For this,
the script here sets its currentState to AIAttackStates.AIAttackState.look_and_destroy:
// tell gun controller to do 'look and destroy'
gunControl.currentState=
AIAttackStates.AIAttackState.look_and_destroy;
}
Any game could use the race controller script. It does not need to be vehicles racing
either; it could be humanoids, robots, or just about anything else that moves. The race con-
troller is attached to all players that need to be tracked in a race, and then the global race
manager talks to them and tracks the race situation. No extra work required!
That said, we need to make sure that our players actually have race controllers attached
to them. AddRaceController() starts by using GameObject.AddComponent() to add an
instance of RaceController to this player:
public void AddRaceController()
{
if(myGO==null)
myGO=gameObject;
Search WWH ::




Custom Search