Graphics Reference
In-Depth Information
// cache a reference to the player controller
myPlayerController= myGO.GetComponent<BasePlayerManager>();
// call base class init
myPlayerController.Init();
The car physics are very simple in this game. In a commercial racing game, there may
be all manner of physics helper scripts running just to keep it upright around corners and
behaving like a real car should. Advanced car physics are beyond the scope of this topic,
so instead we set the physics rigidbody's center of mass in such a way as to encourage the
car to stay upright. If you were to comment out this line and play Metal Vehicle Doom , the
first thing you would notice might be how easy it is to make the car roll over onto its roof
when going around a corner. Changing the center of mass can have strange effects on the
physics, although with a little trial and error, you may be able to reach a good enough level
of stability without too much strangeness in terms of physical reaction:
// with this simple vehicle code, we set the center of mass
// low to try to keep the car from toppling over
myBody.centerOfMass= new Vector3(0,-3.5f,0);
An AudioSource is attached to the player to make a sound for the engine. The
AudioClip referenced by the source should be a looping sound that will sound good when
its pitch is moved up or down depending on the speed of the vehicle:
// see if we can find an engine sound source, if we need to
if( engineSoundSource==null )
{
engineSoundSource= myGO.GetComponent<AudioSource>();
}
The RaceController (as discussed earlier in this chapter) is added next by another
function called AddRaceController():
AddRaceController();
As a matter of protocol, the lap counter within the race controller is reset:
// reset our lap counter
raceControl.ResetLapCounter();
This script is set up to work with the weapon system, and we keep a reference to the
weapon controller component (Standard_SlotWeaponController) in the variable weapon-
Control. This code uses GameObject.GetComponent to find it:
// get a ref to the weapon controller
weaponControl=
myGO.GetComponent<Standard_SlotWeaponController>();
Next in the code, if no player manager component has been set in the Unity edi-
tor Inspector window, the code uses GameObject.GetComponent() to try to find the
BasePlayerManager component. Once a player manager script is found, myDataManager
is set to the data manager object belonging to the player manager.
Search WWH ::




Custom Search