WheelColliderSource-WheelColliderSource

The WheelColliderSource class provides the majority of the wheel system's logic.

Initially the class creates a DummyWheel which is an empty game object attached to the parent the wheel collider. The dummy provides an easy and precise method of getting the forward, up and right directions of the wheel without needing to worry about it's rotation as well as providing a convenient method of getting the wheel's original rest position.

The class runs on the FixedUpdate function which is called just before the physics is resolved and before the Update and LateUpdate loops are processed. In the FixedUpdate function there are several important functions which drive the behavior of the wheel as a while, bellow is a brief description of each with comments on their implementation.

UpdateSuspension
This function casts a ray from the top of the suspension, down it's direction of travel to find any collisions with the ground or other objects. The suspension calculates it's compression based off the collision information and sets a flag as to whether the wheel is in contact with the ground.

UpdateWheel
This function updates the angles of the wheel and dummy based off the steer angle and wheel velocity as well as the position of the wheel based off the suspension state.

The wheel angular velocity is then subjected to a rolling force, a motor force and a brake force. Currently the rolling force is only applied to the wheels which have no motor force as this provided a better result, however in practice it should affect the wheel regardless of it's motor force.

CalculateSlips
This function calculates the forward (longitudinal) and side (lateral) slips acting on the wheel. First is calculates the instantaneous wheel velocity, then separates this into forward and sideways components. The slip is then calculated as the difference in velocities between the ground and the wheel. Given that the wheel is rotating forward however, the forward slip is calculated as the difference in velocity between the wheel surface and the ground.

CalculateForcesFromSlips
This function calculates the forces acting on the wheel resulting from the wheel slip and suspension using the values in the WheelFrictionCurveSource and JointSpringSource classes.

Additional Notes
The Start function searches up the hierarchy to find the closest Rigidbody component. Note that there should only be one Rigidbody per vehicle and it should be in a parent of the WheelColliderSource game objects.

The OnDrawGizmosSelected display is slightly different from the original wheel collider, it also displays a different color depending if the suspension is working within it's limits (green) is fully uncompressed and therefore there is no contact with the ground (blue) or is fully compressed (red).

Change Log

 * 28/10/2010
 * Fixed the search for the WheelCollider's Rigidbody. If there was no Rigidbody found it would crash out with a null pointer exception. It will not output an error message instead.
 * Added a SphereCollider component to the same game object that the WheelCollider is on. This is to fix an issue of the Rigidbody requiring a Collider to function properly. More details can be found on the Collider Issue page.