Anti-roll bars work by transferring some compression force from one spring to the opposite in the same axle. The amount of the transfered force depends on the difference in the suspension travel among the wheels.
So first task is to calculate the suspension travel on each wheel, as well as determine whether is grounded or not. We want the travel value to be between 0.0 (fully compressed) and 1.0 (fully extended):
Q: How can I set a real center of mass for my vehicle?
A: Use non-overlapping box colliders to roughly resemble your vehicle's shape, then move the center towards the position of the engine.
Unity/PhysX calculates the center of mass based on the position and
volume of the GameObject's colliders. Note that overlapping colliders add more
mass at the overlap position.
Typically, you would only need to move the
center of mass a bit towards the position of the engine (front - rear). For
instance, if your vehicle has front engine and its front is oriented in the Z+
direction, you should only need to move the (automatically calculated) center of
mass 1 meter to the front:
code:
function Start ()
{
rigidbody.centerOfMass += Vector3(0, 0, 1.0);
}
Q: Does it really works so good? Could it really be so simple??
A: See it by yourself:
http://www.edy.es/unity/offroader.html
You can have
a good view of how it works by pressing C (change secondary camera), then
pressing B while driving (alternate Sport / Offroad mode). Press N for
completely disabling the stabilizer bars (B to reenable, or Enter to restart
after flipping over).
Ending words....
As this is my first post here, please let me introduce myself. I'm a developer now working on dj software. Years ago, while I was a student two friends and me spent about two years trying to develop games in C++ from scratch (no engines at all), because games was what we wanted to do since kids. As you can imagine, very little to no result could be seen in the screen. I discovered Unity about four weeks ago (had heard of it before, but hadn't look at it) and for me it was like seeing The Light. I could just put things on the screen, click play, and worked! I could then fine-tune the scene by looking at code examples and writing scripts in whatever language used by Unity (two weeks ago I realized I was coding in javascript). Magic! Now I'm about to achieve a personal goal of modeling physically real vehicles. This is what I really wanted to do since I was kid and self-learned to code in Basic with by Commodore 64. Overlander, Powerdrift, Hard Driving, and later 4D Sports Driving, Fatal Racing... now I can easily do it!! Even as hobbyist, I'm purchasing the Unity Pro license just because I love it!
This article is my first contribution to the GREAT Unity community. It's great to have fun by modeling and playing with Unity's vehicle physics.