Monday, 17 February 2014

Adam Jones and the wheel colliders of doom

Wheel colliders... my enemies... or is it? I tried to use them for my racing game, I was sure they will be great. Well, they are great but they need to be properly used. And it's not easy to set them up correctly.

First problem: car goes left or right without steering on a plane ground. It's a common problem as far as I know. I read about on forums and some say it's a bug, other say it's because of incorrect settings. Honestly I don't know, which one is the truth, because as I made experiments with it, some settings were even worse, some of them were almost good... just almost. It still went left or right. I actually found a gif on Unity Forum, which tells me, it's a bug.

Decreasing fixed timestep removes the drift effect, image uploaded to Unity Forum by bitbutter 
It's a serious issue, especially at high speeds! Actually,  I couldn't fix it. I tried to slow down the outer wheel, but it didn't work. I didn't really make any precise calculations for that so...
It's frustrating. I really don't want to make my own wheel collider. Maybe I could do it, but I never tried. Actually, there is a good raycast based wheel collider in Unity Car Tutorial, which I mentioned in previous posts, but they are slow on mobile, however, the suspension system can be useful from that code.

Second problem: finding the right setup is never easy. In case of the custom Pacejka formula based custom collider which comes with Unity Car Tutorial, it was really easy to set up. Hours of tweaking, but I was never far from the right values. It was always enjoyable to drive the car, sometimes it was too easy to drive, sometimes the grip was very low and it made it hard to drive. But it was fun. And at the end I was able to set it up right, just the problem was, this wheel script is slow on mobile platforms.
In case of the Wheel Collider component, I still wasn't able to find the good values. So bad performance vs bad handling... good.

I made a little research and I checked out the Unity Car Tutorial once again because it doesn't have the weird drifting effect and it uses the same wheel colliders! How it's possible? Well, the answer is simple: it doesn't use motorTorque, which means, the car is not powered by the wheels. It's more like a force pushes the car, because exactly that is happening! It uses forces instead of torque, even for braking. Also for turning, it doesn't turn the wheel colliders, but uses rotate.around to rotate the car around a point based on some simple calculations. I must say, these are cool tricks, but mainly for arcade style racing games. In this case, if you want drifting and making doughnuts, it's more about good scripting than pure physics.

I think, I need to go deeper about this topic, because those wheel colliders are the doom for me. Like some untameable animals. They are there, they are useful, but hard to use properly. Or should I make my own custom wheel which is fast even on mobile?

Actually, old games make me think. There is my favourite NFS title, Need For Speed - Porsche Unleashed (also known as Porsche 2000). which is a fairly old game, came out in 2000, and it had a nice, semi-arcade handling. It was running on a 200MHz CPU, a little bit more powerful was recommended, but it ran. I think maybe 6 or 8 cars could race at once. I don't know how it was programmed, but it worked well. Or I should mention earlier F1 titles. That's cool, how they achieved pretty good physics with simple calculations.
Note: old games rock!