Andy73

WheelAlign

Aug 7th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Define the variables used in the script, the Corresponding collider is the wheel collider at the position of
  2. // the visible wheel, the slip prefab is the prefab instantiated when the wheels slide, the rotation value is the
  3. // value used to rotate the wheel around it's axel.
  4. var CorrespondingCollider : WheelCollider;
  5. var SlipPrefab : GameObject;
  6. public var RotationValue : float = 0.0;
  7.  
  8. function Update () {
  9.  
  10.     // define a hit point for the raycast collision
  11.     var hit : RaycastHit;
  12.     // Find the collider's center point, you need to do this because the center of the collider might not actually be
  13.     // the real position if the transform's off.
  14.     var ColliderCenterPoint : Vector3 = CorrespondingCollider.transform.TransformPoint( CorrespondingCollider.center );
  15.    
  16.     // now cast a ray out from the wheel collider's center the distance of the suspension, if it hit something, then use the "hit"
  17.     // variable's data to find where the wheel hit, if it didn't, then se tthe wheel to be fully extended along the suspension.
  18.     if ( Physics.Raycast( ColliderCenterPoint, -CorrespondingCollider.transform.up, hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius ) ) {
  19.         transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius);
  20.     }else{
  21.         transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance);
  22.     }
  23.    
  24.     // now set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value
  25.     // is the rotation around the axle, and the rotation from steering input.
  26.     transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0);
  27.     // increase the rotation value by the rotation speed (in degrees per second)
  28.     RotationValue += CorrespondingCollider.rpm * ( 360/60 ) * Time.deltaTime;
  29.    
  30.     // define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine
  31.     // the slip of the tire.
  32.     var CorrespondingGroundHit : WheelHit;
  33.     CorrespondingCollider.GetGroundHit( CorrespondingGroundHit );
  34.    
  35.     // if the slip of the tire is greater than 2.0, and the slip prefab exists, create an instance of it on the ground at
  36.     // a zero rotation.
  37.     if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > 1.5 ) {
  38.         if ( SlipPrefab ) {
  39.             Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );
  40.         }
  41.     }
  42.    
  43. }
Advertisement
Add Comment
Please, Sign In to add comment