Graphics Reference
In-Depth Information
For a realistic representation of the structure of suspension, the raycast length should
be limited to the length of the suspension and to the radius of the wheel. Starting out from
the center of the WheelCollider, the ray will travel down the collider transform's negative
up vector (i.e., down, as the WheelCollider's transform describes it):
if ( Physics.Raycast( ColliderCenterPoint,
-colliderTransform.up, out hit, CorrespondingCollider.
suspensionDistance + CorrespondingCollider.radius ) ) {
When Physics.Raycast finds a hit, the position for the wheel is the hit point, plus
the wheel collider transform's up vector multiplied by the wheel radius. When the wheel
meshes pivot point is at its center, this should position the wheel in the correct place on
the ground:
myTransform.position= hit.point + (colliderTransform.up *
CorrespondingCollider.radius);
} else {
When Physics.Raycast finds nothing, the wheel position is at its default of the suspen-
sion length down from the WheelCollider's center point:
myTransform.position= ColliderCenterPoint -
( colliderTransform.up * CorrespondingCollider.
suspensionDistance );
}
Now that the position is set for the wheel, the rotation needs to be calculated based
on the WheelCollider's rotation in the 3D space along with some extra code to produce a
rotation angle from the WheelCollider's revolutions per minute (RPM).
The wheel mesh transform's rotation is put together as a result of the WheelCollider's
transform rotation multiplied by a new Euler angle formed with Quaternion.Euler. The
new angle takes the value in the variable RotationValue as its x -axis, the value of the
steerAngle property of the WheelCollider as its y -axis and a zero as its z -axis:
myTransform.rotation= colliderTransform.rotation *
Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0 );
RotationValue is a constantly flowing number; it is never reset once the game
gets going. Instead, RotationValue is incremented by the WheelCollider's RPM value
(CorrespondingCollider.rpm) as it is converted into radians and multiplied by Time.deltaTime
to keep the rotation time based.
// increase the rotation value by the rotation speed (in degrees
// per second)
RotationValue+= CorrespondingCollider.rpm * ( 360 / 60 ) *
Time.deltaTime;
To analyze detailed information from the WheelCollider and its contacts and rela-
tionships with the 3D world, Unity provides a structure called WheelHit.
Search WWH ::




Custom Search