Car Question

So I made a car that as 4 wheel Collider and the two front wheel turn the car while the back wheel move the car. The little problem I’m having is the car flip over really easy. is there anyway i can make it so the car can’t flip over.

Plz help.

Its something i’ve battle with a lot!

To begin with you have to really play around with the wheel collider settings to find the balance primarily on the driving sensation you want and also with the mass & scale of the vehicle.

in order to make the adjustments faster i had made a script where you can add all the wheels collider and adjust the setting with sliders.
with a Debug toggle that will stop adjusting the setting on every frame. (ive put the code at End).

Now other tricks you can do to reduce flipping are:

  • Reduce the center of mass in the rigidbody EX: myRigid.centerOfMass = new Vector3 (myRigid.centerOfMass.x, centerOfMass, myRigid.centerOfMass.z);

  • Depending on the velocity of the car start applying some vertical force to the rigidbody that will push it to the ground EX:myRigid.AddForce (new Vector3 (0, -pushDownForce, 0));

  • Lastly while researching this at a tutorials i have found a little JS script that basically works as an antiroll bar that was quite usefull.
    i dont remember who wrote it… i hope its ok that i am passing the code like that!-)
    Antiroll_Bar.js:

     var WheelL : WheelCollider;
     var WheelR : WheelCollider;
     var AntiRoll = 5000.0;
     
     function FixedUpdate ()
         {
         var hit : WheelHit;
         var travelL = 1.0;
         var travelR = 1.0;
     
         var groundedL = WheelL.GetGroundHit(hit);
         if (groundedL)
             travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
     
         var groundedR = WheelR.GetGroundHit(hit);
         if (groundedR)
             travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
     
         var antiRollForce = (travelL - travelR) * AntiRoll;
     
         if (groundedL)
             rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
                    WheelL.transform.position);	
         if (groundedR)
             rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
                    WheelR.transform.position);	
         }
    

Here the code for the WhellCollider settings :

using UnityEngine;
using System.Collections;
[System.Serializable]
public class WheelSettingCS : MonoBehaviour
{

		public WheelCollider[] wheelColliders;

		public bool debugMode = false;

		[Range(0 , 10)]
		public float
				mass = 1;

		[Range(0 , 2)]
		public float
				distance = 0.2f;

		[Range(0 , 10000)]
		public float
				spingVal = 5500f;

		[Range(0 , 100)]
		public float
				spingDamper = 50f;

		[Range(-100 , 100)]
		public float
				position = 0;

		[Range(0 , 5)]
		public float
				ffextremumSlip = 1;

		[Range(0 , 40000)]
		public float
				ffextremumValue = 20000;

		[Range(0 , 4)]
		public float
				ffeasymptoteSlip = 2;

		[Range(0 , 20000)]
		public float
				ffasymptoteValue = 10000;

		[Range(0 , 1)]
		public float
				ffstiffness = 0.092f;

		[Range(0 , 5)]
		public float
				sfextremumSlip = 1;

		[Range(0 , 40000)]
		public float
				sfextremumValue = 20000;

		[Range(0 , 4)]
		public float
				sfeasymptoteSlip = 2;

		[Range(0 , 20000)]
		public float
				sfasymptoteValue = 10000;

		[Range(0 , 1)]
		public float
				sfstiffness = 0.092f; 


		// Use this for initialization
		void OnEnable ()
		{
				FixSpringSettings ();
		}
	
		// Update is called once per frame
		void FixedUpdate ()
		{
				if (debugMode)
						FixSpringSettings ();
		}


		void FixSpringSettings ()
		{
				for (int i = 0; i < wheelColliders.Length; i++) {
			
						WheelCollider whellCol = wheelColliders *;*
  •  				whellCol.suspensionDistance = distance;*
    
  •  				whellCol.mass = mass;*
    
  •  				JointSpring newSuspesion = new JointSpring ();*
    
  •  				newSuspesion.spring = spingVal;*
    
  •  				newSuspesion.damper = spingDamper;*
    
  •  				newSuspesion.targetPosition = position;* 
    
  •  				whellCol.suspensionSpring = newSuspesion;*
    
  •  				WheelFrictionCurve newFwdFriction = new WheelFrictionCurve ();*
    
  •  				newFwdFriction.extremumSlip = ffextremumSlip;*
    
  •  				newFwdFriction.extremumValue = ffextremumValue;*
    
  •  				newFwdFriction.asymptoteSlip = ffeasymptoteSlip;*
    
  •  				newFwdFriction.asymptoteValue = ffasymptoteValue;*
    
  •  				newFwdFriction.stiffness = ffstiffness;*
    
  •  				whellCol.forwardFriction = newFwdFriction;*
    
  •  				WheelFrictionCurve newSideFriction = new WheelFrictionCurve ();*
    
  •  				newSideFriction.extremumSlip = sfextremumSlip;*
    
  •  				newSideFriction.extremumValue = sfextremumValue;*
    
  •  				newSideFriction.asymptoteSlip = sfeasymptoteSlip;*
    
  •  				newSideFriction.asymptoteValue = sfasymptoteValue;*
    
  •  				newSideFriction.stiffness = sfstiffness;*
    
  •  				whellCol.sidewaysFriction = newSideFriction;*
    
  •  		}*
    
  •  }*
    

}

Read this forum page about How to make a physically real, stable car using WheelColliders has really good information on how to make a physically stable car.