Graphics Reference
In-Depth Information
// add a race controller script to our object
raceControl= myGO.AddComponent<RaceController>();
It is also a good idea to grab the ID from the race controller to use elsewhere, keep-
ing any ID numbers the same. racerID is an integer that gets set to the ID returned from
raceControl.GetID():
// grab our racer ID from the race control script, so we
// don't have to look it up whenever we communicate with the
// global race controller
racerID=raceControl.GetID();
Every half second, this player script will call upon UpdateRacePosition() to update its
position from the global race manager. An InvokeRepeating call is used to set this up on a
recurring basis throughout the game:
// set up a repeating invoke to update our race position,
// rather than calling it every single tick or update
InvokeRepeating("UpdateRacePosition",0, 0.5f);
}
UpdateRacePosition() asks the global race manager which position this player is in,
and then stores it in the variable myRacePosition:
public void UpdateRacePosition()
{
// grab our race position from the global race manager
myRacePosition=GlobalRaceManager.Instance.GetPosition(racerID );
}
The UpdatePhysics() function is called each FixedUpdate() by the BaseVehicle script.
The function here is an override to the original UpdatePhysics() in BaseVehicle.cs, but
even though that original function has been overridden, it still exists and may still be
called. When the script is allowed to control the vehicle, base.UpdatePhysics() is used to
call the UpdatePhysics() function in that BaseVehicle.cs script:
public override void UpdatePhysics ()
{
if(canControl)
base.UpdatePhysics();
At low speeds, it can be difficult to turn the vehicle quickly. When the user crashes
into a wall, that slow turning speed can make the game a very frustrating experience.
The main aim of this overridden version of UpdatePhysics() is to add some turn helper
force, to make the vehicle turn faster at lower speeds so that getting out of a crash may be
easier. It also means that the vehicle can do some rather fun donuts on the track, but that's
another story…
Here we check the value of mySpeed (which, of course, holds the player's current
movement speed) and see whether it is below the value of TurnTorqueHelperMaxSpeed.
The intention is that its value will be set in the Unity editor Inspector window. If mySpeed
is greater than TurnTorqueHelperMaxSpeed, no helper force will be applied to the vehicle.
If it is traveling below that threshold, rigidBody.AddTorque() is used to turn the vehicle:
Search WWH ::




Custom Search