# 2D Car tutorials or experience?

I am currently playing around with a 2D car game for fun. It’s going ok but still it’s hard to get the physics right. Does anyone have experience with this or know a good tutorial about it?

What I have done currently is created a box that is 4 by 1.5 units. I have also created two wheel colliders with a radius of 0.25.

To make the car stable I have created a script that allows me to move around the centre of mass of the car. This makes the car stable (when I lower it, almost down to the ground) but it behaves strangely when toppling over landing on the roof of the car. This can sometimes make the car topple back up because of the center of mass. What would be the best/correct way of solving that? In real life a car has a low center of gravity however I am pretty sure they don’t topple back when landing on the roof very easily

It should also be possible to tilt the car in the air. What would be the best solution for this, using AddTorque or perhaps moving the center of gravity?

Ofcourse there are other problems also, such as the cars wheels sometimes falling through the ground etc etc.

So any experience out there on building a nice 2D car with Unity?

If the car topples back after rolling over then it implies that the centre of mass is located underneath the car (there’s nothing in the rigidbody definition to stop you doing this and indeed it is sometimes useful). If you tweak the centre of mass a bit, you should be able to get it low enough for stability without having it underneath the road.

Tilting the car in the air is best done with AddTorque or AddRelativeTorque - changing the centre of mass in mid flight will probably give strange results and won’t impart much rotation to the car.

As for the wheel colliders falling through the ground, this can often happen if the car lands too heavily or if it tips over and the wheel’s attachment point is quickly rotated under the ground. An easy way to stop this happening is to put an invisible capsule collider at roughly the same location as the wheel. The collider should be aligned with the centre of the wheel but its radius should be about half that of the wheel (ie, the collider shouldn’t drag along the ground on a normal, smooth road). Also, you should use a slippery physic material on the capsules to stop them gripping bumps in the road if they make contact.

Just saw this reply now, thank you very much Andeeee.

The car is coming along nicely actually, your tips was of great help!

I however still have one problem. I’m moving the wheels with the help of wheelCollider.GetGroundHit. This works but the hit position sometimes jump up to a negative value causing the wheels to flicker. I can see they are in the correct position but at all times they jump up to a strange position almost back up to their original position. What is causing this? The bool returned is ofcourse true when this happens. What is the correct way of eliminating this. I could ofcourse make some kind of check that does not move the wheels during a certain threshold but that just seems wrong. I would rather like to find the real reason for this happening!

Thanks!