Search Unity

Resolved The Dark Art of Unity Wheel Colliders - What Unity Doesn't Tell You.

Discussion in 'Physics' started by Peeling, May 13, 2021.

  1. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    I've been working on the physics for a racing game for a while now, and (like many) struggling to get Unity's wheel colliders to behave themselves. I've recently taken some big steps forward, and wanted to share a couple of tricks I discovered along the way, some of them less intuitive than others.

    Background : How Unity Wheel Colliders Determine Grip

    If you've done anything with wheel colliders you'll already know about the forward and sideways Friction curves, which relate the amount of opposing force generated to the slip the wheel is experiencing. Skip this section if you're already comfortable with the theory of how they work. Or don't - you might be wrong! I was, no end of times.

    Focusing on Sideways friction only, there are five values to configure: Extremum Slip, Extremum Value, Asymptote Slip, Asymptote Value, and Stiffness. There are easy-to-find diagrams illustrating the intended shape of the curve, so I won't reproduce them here.

    (Note carefully, as you read on, whether I'm talking about extremum SLIP or extremum VALUE.)

    Before I get into how those values define behaviour, what's 'slip'? In terms of sideways friction, it's a measurement of how much the wheel is sliding perpendicular to its direction of roll.

    Imagine you are driving in a straight line at 10mph. Sideways slip on all tyres will be ~0. Now imagine you suddenly turn the front wheels 20 degrees right. When Unity moves the car forward now, still in a straight line, the front wheels will be forced to scrape sideways a little. Unity measures that scraping or 'slip', feeds it through the friction curve, multiplies it by the force currently pressing the wheel into the ground (via its own weight and the suspension), and applies that force laterally to push the wheel (and hence the nose of the car) sideways.

    Okay, but what is this 'friction curve'?

    Obviously enough, the curve starts at (0,0): zero slip = zero lateral force.

    Extremum Slip and Extremum Value represent the next point on the curve. The lateral force applied to the car will rise more or less linearly as measured slip increases, up to (Extremem Slip , Extremum Value).

    In order to visualise that, imagine repeating the above experiment, but at 20mph instead of 10mph. When you turn the steering (by the same amount) Unity will now measure exactly twice the amount of slip (twice as fast = twice the distance travelled = twice the slip). Assuming that amount of slip is still less than the Extremum, the result will be exactly twice as much lateral force pushing the nose of the car sideways - which is (more or less) exactly what is needed to ensure the car follows the same arc at 20mph as it did at 10mph.

    Now let's try to take the same corner at 100mph. Instead of twice as much slip, Unity now measures ten times as much. Let's assume that exceeds the Extremum Slip, so Unity is going to be looking beyond the 'hump' in the curve to determine its response.

    Typically, the Asymptote value is quite a bit lower, which means that the wheel will not exert the tenfold greater force required to maintain roughly the same arc. The result is understeer. This is Unity's approximation of the effect of exceeding tyre grip and skidding.

    What about 'stiffness', then?

    Although I said the car would follow roughly the same curve at both 10 and 20mph in the thought experiment above, I didn't say it would follow the true 'on rails' curve described by the direction of the wheels. By itself, the Extremum value you choose might not be sufficient, when multiplied by the force between wheel and road, to push the nose of the car sideways as though it were on rails.

    Stiffness is a simple multiplier, which lets you tweak the scale of the forces generated by the wheel. The higher the stiffness, the harder the wheel will push sideways.

    If stiffness is set too high, forces will exceed those needed to turn the car 'on rails' and it will become unstable and jitter from side to side.

    If stiffness is set too low, the forces will noticeably fall short of what's required, and significant constant understeer will result.

    NB: An extremum value of 1 and a stiffness of 10 is the same as an extremum value of 10 and a stiffness of 1. I always set my extremum value to 1 and play around with Stiffness. If you catch my drift.

    Okay, that's how Unity Wheel Colliders work. So what?

    Now that we have these entirely sensible-seeming basics firmly in hand, we can start learning all the ways in which they're super bogus and need fettling.

    NOTE: If you are experimenting with wheel colliders, always put a debug readout on the screen to display current sideways slip divided by Extremum slip on each wheel.

    This is really important, because understeer caused by stiffness being too low 'feels' exactly the same as understeer caused by skidding. Only a readout telling you whether you are above or below Extremum slip will tell you what's going on and allow you to correct it properly.

    With that in place, the first counter-intuitive lesson is that rear tyre configuration can be super important for avoiding understeer.

    Understeering? Try stiffening up the rear tyres and lowering their Extremum Slip.

    On the face of it, this makes no sense at all. Why should increasing rear tyre grip and responsiveness help prevent UNDERsteer?

    The answer comes from thinking about how Unity measures slip. When you turn the front wheels, the lateral forces push the nose of the car sideways. That rotates the car - but only partially alters its direction of travel. If the rear tyres are very soft and forgiving (LOW stiffness, or a HIGH extremum slip, meaning a lot of slip is needed to exert a significant force), the rear end of the car will continue pretty much straight on. You would think this would result in OVERsteer, but what can happen instead is that when the extra rotation of the car is added to the angle of the front wheels (so they're now even more sharply angled to the direction of travel), the amount of slip they experience can pop them over their extremum and start a front-wheel skid. Meanwhile, the rear tyres are nowhere near THEIR extremum, so they hang in there. The result feels like understeer (often with a lot of tail-wagging), even though it's the rear tyres that are to blame.

    Reducing rear-tyre extremum slip and increasing stiffness helps them react more swiftly to changes in direction, preventing car rotation from accumulating.

    Understeering? Try reducing the maximum lock of your steering at high speeds.

    This is less counter-intuitive and more of just a big face-palm if you forget. Large angles of lock are only useful at very low speeds. At racing speeds, ten degrees is probably plenty. You can often completely cure understeer and get your cars around corners faster than you would dream possible just by reducing the maximum steering angle as speed increases. Think again about how Unity measures slip: the sharper the angle against the direction of travel, the greater the slip, and if slip exceeds the extremum, you're into skiddy time. It's better to turn the wheels 10 degrees and make a 10 degree turn than it is to turn them 20 degrees and go straight on into a wall.

    Want to make your steering more forgiving? Set the asymptote VALUE to the same as the extremum VALUE

    Especially when playing with controllers, it can be difficult to gauge exactly how much steering to do through a given corner. Also, the instinct when approaching a corner too quickly is to flick the stick hard over. If the asymptote value is low, this will result in catastrophic understeer.

    One way to solve this is discussed above: lower the steering lock range at higher speeds. Another is to bring the asymptote value closer to the extremum value. The more you do, the more forgiving your car will be of excessive steering input. Make them the same, and the car will always follow the tightest arc the tyres are physically capable of, regardless of how much the player oversteers. This is generally good for arcadey racers, but you might want to dial in a small amount of penalty to reward more skilful drivers, and even more of a penalty if you know the player is using a steering wheel accessory (which makes hitting the steering sweet spot far easier).

    Thinking of applying downforce? Physicists are furious about this one simple trick!

    One of the most oft-repeated bits of advice is to add fake aerodynamics to vehicles in order to help them stick to the road. A simple script to measure forward velocity and apply a corresponding downward force at the front and/or rear of the car can - at first glance - seem to solve all your car-handling woes.

    There's just one problem: it doesn't actually work, and attentive readers will already have spotted why.

    In the real world, increasing the contact force between rubber and road increases grip. It increases the amount of lateral force the tyre is able to counteract, making it less willing to slide.

    In Unity tyre physics, that doesn't happen.

    In Unity physics, downforce (as transmitted to the tyre via the suspension) is applied at the end of the grip calculations, as a multiplier. The determination of whether the tyre is gripping or skidding is made without any reference to contact force. It's calculated based only on the angle of the tyre to the direction of current travel, and speed.

    Let's go back to our earlier thought experiment. We still want to be able to corner at 100mph, so we add downforce to our car equal to its weight, thinking that will help. What happens?

    Well, when we angle the wheels, we are still going ten times faster than we were at 10mph, which means Unity will still measure 10x the slip on the front tyres, which will still exceed their extremum slip and still result in a skid. It won't be as bad as before, because the downforce doubles the result of Unity's sums at the end, but it will probably still be well below what's necessary.

    Our natural reaction, observing this, is to assume we didn't add enough downforce. So we ramp it up and up until the multiplier on the weak, skiddy force gets so big that we get around the corner as we had hoped.

    Why is this a problem?

    It's a problem because we are now making it around that particular corner at that particular speed in the anti-sweet-spot of the tyre's grip. That's the worst they can possibly perform. Which means that if we ever hit the sweet spot, the car is going to snap our goddamn necks. And that is what we will find somewhere else on your track: we'll be able to round a slightly different bend at twice or three times realistic speeds. We'll also find the steering intolerably twitchy at high speeds, and end up going down other rabbit-holes to fix that.

    You can, and I have, spent days going around and around like that, never managing to make the car feel right in every circumstance. Fortunately, there is a sort-of proper solution, and it's a pretty simple one, too.

    The solution is to dynamically multiply the front tyres' starting sideways extremum slip and asymptote slip by:

    (1 + (total extra downforce / car weight))

    This means that if the extra downforce is equal to the car's weight, the extremum slip (NOT VALUE) and asymptote slip (NOT VALUE) of the front tyres will be twice what they are when the car is at rest. Consequently, the tyres will endure twice as much slip before starting to skid. And since downforce acts as a multiplier at the end of the equation too, that 'twice as much slip' will generate a response of 'twice as much force' - which is exactly what's needed.

    Can you summarise all that? What should I actually DO?

    Sure. In this order:

    1. Make sure you have readouts so you can see when (current slip divided by extremum slip) exceeds 1. That will tell you whether the drift or understeer you're experiencing is due to actual skidding or overly soft tyres.

    2. Limit your car's steering lock at higher speeds. Particularly with keys or controllers, it's very very difficult for a player to input the actual amount of steering needed to negotiate a corner 'on rails'. I only realised this when I forced myself to steer no more than 50% of joystick travel, and found myself zooming around corners I had previously been crashing out of. Having a graceful narrowing of the steering lock with increasing speed will help no end.

    3. Tighten up those rear tyres. Soft-but-grippy rear tyres are a recipe for ugly understeer, as the car initially slops into oversteer, breaking the front tyres' grip while the rears continue to hang on. Try REDUCING rear extremum slip point and/or INCREASING rear stiffness.

    4. If you're applying speed-related downforce, don't forget to also dynamically scale front tyre extremum slip point and asymptote slip point, otherwise you're just applying downforce to a car that's still technically skidding.

    Hope that's of some help!
     
    Last edited: May 14, 2021
  2. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    this is a nice insightful info.

    used dynamic wheel values before, but reading this it sounds like it was badly designed, and the values should be modified dynamically realtime (not just switching values) based on a couple of factors.

    the problem with limiting steering on high speed is that skidding is no go.

    wasn't using downforce, but might be useful if switching on/off based on checking wheel contacts
    Code (CSharp):
    1.  current slip divided by extremum slip
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Very good article!
    This is really interesting. Good to know that at least the units in the X axis of the curves is the same as the slip in WheelHit.

    Still, it's interesting to note that the extremum-asymptote curve is no longer used internally by PhysX. These curves are now some kind of undocumented wrapper in the Unity side just to preserve the previous curves. Indeed, only longitudinal friction uses something similar to an extremum-asymtote curve, while the lateral friction is configured using two float values only.

    Here's an article describing what we know about the WheelCollider's internals:
    https://forum.unity.com/threads/can...ymptote-and-extremum-are.947193/#post-6179013

    Additionally to the slip, I'd recommend monitoring the suspension compression. Bad unrealistic things happen when suspension is fully compressed. That might explain why aerodynamic forces produce unexpected effects. Suspension spring should be stiff enough to support both the weight transfers and the aerodynamic forces.
     
    KarlGG and keeponshading like this.
  4. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    I'll just drop it in here
     
    Deleted User, ch715t0 and Edy like this.
  5. Davidbillmanoy

    Davidbillmanoy

    Joined:
    Jul 7, 2014
    Posts:
    120
    For simulation driving: I want to make the cars drive/behave just like in Gran Turismo games.

    For drifting: I want to make the cars drift just like in real life which is real, not arcade style which is fake.
     
  6. PurpleGooper

    PurpleGooper

    Joined:
    Jul 9, 2019
    Posts:
    7
    Nice tips!
    Do you have any suggestions for adding actual standard colliders to the tires on top of the WheelCollider? I have this problem where if the vehicle flips and the WheelCollider touches the ground sideways, it shoots the vehicle upwards ( in an attempt to rebalance it i assume).
     
  7. Sir_Ozi

    Sir_Ozi

    Joined:
    Mar 31, 2013
    Posts:
    6
    I know this is old thread, but I'm in search of the web for my problem, that probably you encounter as well, and with your insights, maybe have a sollution.
    I keep losing grip going foward. Anything more than like 100 force of motortorque keep driving wheels spinning and 100 force make car too slow for any kind of racing game. Weight of the car 1000, weight of the wheel 20.
    Problem is, I can't just more ex value, or stiffness, because after like 4, the car jiggle left and right.
    edit:
    Of course the moment I ask online I thought about sollution.
    Problem on my side is how I take wheel rpm to callculate engine rpm, I made simple throtle*power and I don't see the shaking. Gonna find a new way for the engine.
     
    Last edited: Nov 20, 2021
  8. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    Heh, yeah: I feel as though I've spent the last eleven months in orbit around an understanding of how to get good behaviour out of Unity's wheel coliders :)

    For context: we're making an F1-style driving game with real life speeds of up to 250mph, rapid acceleration, deceleration and cornering. In addition to the tips above, here's what I've discerned along the way:

    1. Realistic vehicle mass proved unstable. We've ended up going with 1/10th scale Rigidbody mass (so around 75kg)
    2. Here's our slowest car rear wheel settings when the car is stationary:
    upload_2021-11-20_20-56-37.png

    and here they are when we're doing 100mph:

    upload_2021-11-20_20-57-38.png

    The differences are in the extremum slip, asymptote slip, and stiffness values, and those changes are based on downforce from the rear wing, as described in my original post. Remember: adding downforce alone does not change the physics of the tyre! You should always scale the extremum slip and asymptote slip according to the extra downforce applied, otherwise Unity will still think you're skidding.

    3. I check side slip, and reduce power to the wheels if it exceeds the Extremum Slip value. This serves as a crude form of traction control and helps prevent the car fishtailing out of a corner when you accelerate.

    4. It's generally better to have the rear wheel sideways stiffness higher than the front. This will make the car less prone to spinning out.

    5. I mention this in the OP, but I'm gonna say it again here: make your Asymptote value the same as the Extremum value - at least while you're calibrating your setup. You say your tyres are spinning with 100 motor force - I bet this is because your asymptote value is low.

    6. Reduce steering lock at higher speeds. I cannot overstate the importance of this. Scale down your left/right steering as speed increases, and you'll notice an immediate difference.
     
    Fabi_ash, ch715t0 and Sir_Ozi like this.
  9. PatrickTalksALot

    PatrickTalksALot

    Joined:
    Dec 8, 2020
    Posts:
    1
    This post saved my life(that's a bit dramatic). I was going through the journey of learning the basics of wheel colliders in unity. I didn't want something super realistic, but also not something crazy arcady. I just wanted something that felt really nice to drive with a lot of control while also still having some challenge on a tight track. I first tried some basic free controllers on the asset store, and they all sucked. I also watched a bunch of tutorials and they gave me the same sucky results. So I finally decided, if you want to do something right, do it yourself. I began messing around with wheel colliders and in one day I got the best results I have seen yet by far. Of course then everything broke and I have no idea why, but I can tell that I would never have gotten what I really wanted if I continued doing what I was doing. It solved some of my problems, but just lead to more. Hopefully with the help of this forum post I will be able to find that sweet spot and really get a nice controller. Thanks for all the awesome info! Might make a little youtube video showing off my controller and a tutorial because boy did I need that when I was looking on youtube.
     
    Peeling likes this.
  10. stealthman64

    stealthman64

    Joined:
    Apr 29, 2021
    Posts:
    1
    This is brilliant, thank you so much. My game is coming together now that I can get the car to "feel right".
     
  11. Deleted User

    Deleted User

    Guest

    This Helped So much Thanks