Graphics Reference
In-Depth Information
To keep everything tidy, all of the physics functionality is called from FixedUpdate(),
but it actually resides in the UpdatePhysics() function:
public virtual void FixedUpdate()
{
UpdatePhysics();
}
Changing the pitch of the engine AudioSource object makes a convincing motor
sound. As it may be possible for the variable mySpeed to be negative, Mathf.Abs is used,
as well as an arbitrary multiplier to bring the number down to something that changes the
pitch in a satisfactory manner. There's no science here! The number was decided by trial
and error, just adjusting its value until the motor sounded right:
public virtual void UpdateEngineAudio()
{
// this is just a 'made up' multiplier value applied to
// mySpeed.
engineSoundSource.pitch= 0.5f + ( Mathf.Abs( mySpeed ) *
0.005f );
}
The real guts of the script may be found in the UpdatePhysics() function. CheckLock()
is called first, which takes care of holding the vehicle in place when the lock is on (the vari-
able is Locked set to true):
public virtual void UpdatePhysics()
{
CheckLock();
velo is a variable holding the velocity of the car's rigidbody:
// grab the velocity of the rigidbody and convert it into
// flat velocity (remove the Y)
velo= myBody.angularVelocity;
The next task is to find out the velocity in the local coordinate space, remove its y -axis,
and use its value along the z -axis to put into the variable mySpeed:
// convert the velocity to local space so we can see how fast we're
// moving forward (along the local z axis)
velo= transform.InverseTransformDirection(myBody.velocity);
flatVelo.x= velo.x;
flatVelo.y= 0;
flatVelo.z= velo.z;
// work out our current forward speed
mySpeed= velo.z;
mySpeed is then checked to see whether its value is less than 2. When the speed is
slow and the brake key is held down, brakeTorque is no longer applied to the wheels;
instead, a reverse amount of the maximum amount of brake torque (brakeMax) is
applied to make the vehicle move backwards. Note that brakeMax is both the maxi-
mum amount of torque applied into the brakeTorque property of the WheelColliders
Search WWH ::




Custom Search