Graphics Reference
In-Depth Information
block of code looks to see whether targetAngle is −90° or 90° out and will set the going-
WrongWay Boolean variable accordingly:
if(targetAngle<-90 || targetAngle>90){
goingWrongWay=true;
} else {
goingWrongWay=false;
If a vehicle is going the wrong way for a set amount of time, it will be automatically
respawned by the game code. timeWrongWayStarted is used to hold the time at which the
vehicle heading in the wrong way is first detected.
When the code no longer detects the vehicle going in the wrong direction, time-
WrongWayStarted is set to −1 so as to avoid a respawn:
timeWrongWayStarted=-1;
}
timeWrongWayStarted is, in effect, a timestamp. When the detection of a wrong way
first happens, oldWrongWay will not be equal to goingWrongWay so it is safe to start the
timestamp at this point. timeWrongWayStarted is set to Time.time and it will be com-
pared to the current value of Time.time (in the CarController_MVD.cs script) to see how
long has passed since the wrong way began:
if(oldWrongWay!=goingWrongWay)
{
// store the current time
timeWrongWayStarted= Time.time;
}
It's the end of the CheckWrongWay() function, and oldWrongWay may now be set to
the current value of goingWrongWay:
oldWrongWay=goingWrongWay;
}
As discussed earlier in this section, the current lap will not be increased as soon as the
player reaches the end of its waypoints. The player also needs to cross the start/finish line
before the lap counter will be updated, and a simple trigger collider is used on an invisible
box crossing the track. When the player hits the trigger, the OnTriggerEnter function is
automatically called by the Unity engine:
public void OnTriggerEnter( Collider other )
{
Using a string to find out whether or not the trigger is hit is a bit of an inefficient way of
doing things, but in this case, as there are hardly any triggers in the game, it should have little
or no noticeable impact on performance. If you were building a game with many triggers,
I would recommend switching this out and using either layer checking (with constants) or
another method that does not involve string checking every time a trigger gets hit.
When OnTriggerEnter is called, the call contains information about the Collider that
the transform has hit. The variable other is of type Collider, which means it has all the
properties of a Collider and may be accessed as one. The function starts out by looking at
Search WWH ::




Custom Search