Graphics Reference
In-Depth Information
There may be occasions where other scripts need to get this player's unique ID num-
ber, which is why the GetID() function returns the value of myID:
public int GetID ()
{
return myID;
}
When the player passes by all of the waypoints around the track, the variable isLapDone
is set to true. This Boolean is used when the player hits the trigger on the start/finish line
to see whether or not the lap should be increased. Increasing laps in this way (rather than
just incrementing the current lap counter at the start/finish line) ensures that the player
has driven all the way around the track and crosses the finish line:
public bool IsLapDone ()
{
return isLapDone;
}
RaceFinished() is the function responsible for finding out the player's final race posi-
tion and for telling the player when it is time to stop racing.
First, our internal variables are set up for use in other scripts, that is, isFinished, race
Running, and finalRacePosition:
public void RaceFinished ()
{
isFinished=true;
raceRunning=true;
finalRacePosition gets its value by hitting up the GlobalRaceManager for its race posi-
tion via GetPosition (passing in this player's ID from myID):
// find out which position we finished in
int finalRacePosition=
GlobalRaceManager.Instance.GetPosition( myID );
To tell the player that the race has finished, this script uses Unity's function gameOb-
ject.SendMessageUpwards(). What this does is send a message from this script to this
gameObject and every ancestor of it. If there is a component script attached to the same
gameObject this script is attached to, containing a function naming the message being
sent, in this case, PlayerFinishedRace(), then it will be called.
This part of the code could have stored a reference to the player's control script directly,
but that would mean hard coding the player script, which reduces the overall flexibility of this
script. For example, the player control script for this game is named PlayerController_MVD.
The player control script for the example game Tank Bat tl e is named PlayerController_TB. If
we wanted to make a reference to the player's script for this game, it would need to be held in
a variable of type PlayerController_MVD, and for the Tank Bat tl e game, it would need to be
in a variable of a different type: PlayerController_TB. To make this script work for both games
would mean that we needed to create either two separate references to the two different player
scripts or two different versions of the script. Instead, SendMessageUpwards() will send out
the message to everything without caring about what type of script it is talking to. Just in case
Search WWH ::




Custom Search