Strange WheelCollider Behavior

Hi all, this is a problem I found when messing around with a “Car” script I found in the forum. I used it several times and all seems to work fine until I tried to adjust some special car behaviors with very soft suspension.

Then I tried to isolate the problem, simplify the script and the models and this is what I finished with. As you can see in the picture, the cars are very simple models, a block and four spheres created inside Unity.

To the car in the left, I add a rigidbody and four wheel colliders, one for each sphere. To the car on the right I add another rigidbody with the exact same parameters, but the wheel colliders are created with the “CreateWheelColliders” script, with the exact same parameters as in the first case (mass, suspension, springs, etc.).

So, Why the car in the right is clearly banking while the one on the left is evenly standing on the floor?.

Actually, the physics for the two are the same, despite that in one case the colliders are created manually and in the other by a script.

I include the folder of the project “CarBug.zip” , to see the difference you only must to press play.

Can you see it?, Maybe is a problem of my computer?.

Thank you for your interest.

82434–3192–$carbug_638.zip (658 KB)

I’ve had this problem before too. Easy fix though:

Change this:

for(w in wheels)
	{
		if(w.graphic==null)
			Debug.Log("You need to assign all four wheels for the car script!");
		if(!w.graphic.transform.IsChildOf(transform))	
			Debug.Log("Wheels need to be children of the Object with the car script");
			

		//create collider
		colliderObject = new GameObject("WheelCollider");
		colliderObject.transform.parent = transform;
		colliderObject.transform.localPosition = w.graphic.localPosition;
		w.coll = colliderObject.AddComponent(WheelCollider);
		w.coll.suspensionDistance = suspensionDistancia;
		w.coll.suspensionSpring.spring = springs;
		w.coll.suspensionSpring.damper = dampers;
		w.coll.radius = wheelRadius;
	}

To this:

for(w in wheels)
	{
		if(w.graphic==null)
			Debug.Log("You need to assign all four wheels for the car script!");
		if(!w.graphic.transform.IsChildOf(transform))	
			Debug.Log("Wheels need to be children of the Object with the car script");
			

		//create collider
		colliderObject = new GameObject("WheelCollider");
		colliderObject.transform.parent = transform;
		colliderObject.transform.localPosition = w.graphic.localPosition;
		w.coll = colliderObject.AddComponent(WheelCollider);
	}
	
	for( w in wheels )
	{
		w.coll.suspensionDistance = suspensionDistancia;
		w.coll.suspensionSpring.spring = springs;
		w.coll.suspensionSpring.damper = dampers;
		w.coll.radius = wheelRadius;
	}

It has something to do with the mass getting recalculated… or something. Like has been said on the forums before: the wheelcollider is a black box.

I also set the mass of the WheelCollider in that second loop too.

I am still having a hell of a time getting the slip to work nicely. You would think doing a burnout should be possible without constantly feeling like you are driving on ice.

None of this is Unity’s fault, the WheelCollider appears to be directly from PhysX (NxWheelShape). More discussion:
http://developer.nvidia.com/forums/index.php?showtopic=1714&mode=linear

Easy to make arcade-like cars, tricky to make them more realistic.

Thank you very much Proton, you save my day.

I was wondering it must be something related to the center of gravity.

Finally it seems to be the “WheelRadius” the problem, this code also works:

for(w in wheels) 
   { 
      if(w.graphic==null) 
         Debug.Log("You need to assign all four wheels for the car script!"); 
      if(!w.graphic.transform.IsChildOf(transform))    
         Debug.Log("Wheels need to be children of the Object with the car script"); 
          

      //create collider 
      colliderObject = new GameObject("WheelCollider"); 
      colliderObject.transform.parent = transform; 
      colliderObject.transform.localPosition = w.graphic.localPosition; 
      w.coll = colliderObject.AddComponent(WheelCollider); 
      
      w.coll.suspensionDistance = suspensionDistancia;
      w.coll.suspensionSpring.spring = springs; 
      w.coll.suspensionSpring.damper = dampers; 
      
   } 
    
   for( w in wheels ) 
   {    
      w.coll.radius = wheelRadius;
   }

Thank you again

This problem took me several frustrating hours, apparently missed this post in an earlier search but now thanks to your answer my car doesn’t topple over to the right anymore.
A good tutorial with wheel colliders would be welcome, maybe I clean up my code and post it as sort of tutorial/starting point.

I’m all in on this one! A good tutorial on how to make a car with wheel-colliders would make a lot of people happy and I’d be very happy to contribute. That is once I figured out how to properly work that thing. :confused: But I feel that I’m oh so slowly getting closer…

Hi,

I got wheelcolliders working (and really use the wheelcolliders to ride instead of putting forces on the vehicle). As others seem to struggle with it I will upload my test project. I meant to upload a simpler version of the project but somehow many of my prefabs got broken when I reopened the project.

The script that really counts here is the SimpleCar.js, it got lot’s of room for improvement but at least I put some comments in it so hopefully it’s enough to get starting. The models are made with Cheetah3D so I don’t know how well it runs if you haven’t got it (should be fine I think).

The car seems to topple over quite easily, if someone can fix this without cheating by putting extra forces on the car body I would like to know :wink:

Feel free to do whatever you want with the project, some models/graphics are from an earlier low-budget game we made with Erlicht3D so please don’t use those in commercial projects (not likely you want it because the style and simplicity limit the use outside the original game ;-).

I’m now evaluating Unity3D for a new game (that’s why the project is kind of a smørgasbord) and although it seems to have it’s share of bugs, not having to use Visual Studio and not having to worry too much about different hardware seems to be quite promising, even although it means I have to start all over again. What you see now is the work of a few evenings so it seems to live to the promise of rapid results.

82899–3218–$vogeltest_142.zip (3.25 MB)

That’s a cute little program.

Another weight issue in there though.

Move this line:

rigidbody.centerOfMass += shiftCenter;

To the end of the Start() function after the wheel colliders are created.

Then set the shiftCenter to (0,-0.3,0) in the inspector. You will have a tougher time flipping the car.

Thanks Proton, indeed I should have thought of that, makes quite a difference! It’s a pity we’ve to guess in what order things must be done. I would have preferred if the system didn’t try to be intelligent but that you had to tell to recalculate the physics characteristics after setting all parameters, same goes for Mesh, got some weird problems with them to (see the refresh hack in Update in MeshTest.js, without it planes keep disappearing in the player version of the game).

I’ve still got a long way to go, but I already like the response on this forum, likely I’ll ditch irrlicht for unity and shell out the upgrade from indie to pro (best thing about irrlicht is that it’s free (and reasonably cross-platform, including Linux hint hint) :wink:

At last I done it, real car physics, see my post for details.

http://forum.unity3d.com/viewtopic.php?t=12740

Thanks so much for posting this, I adapted it to my game. One question though, why are you creating the second wheel collider in start(), after the SetWheelParams() call (in the posted fix above)? After some head banging I realized that the collider being used during runtime was the one created in SetWheelParams(), and that wasn’t getting adjusted to the correct wheel radius in the second for loop. My fix was to remove the create statements in the for loop.

Of course there were a number of differences in my implementation (object offsets, wheel grouping, necessary torque (blew the dang thing clear across the terrain first time I got it to work right :lol: ), etc…).

Just curious.

Thanks,

Galen