Posted January 30, 2021 by BigHandInSky
So coming into 7DFPS (way back in the distant past of 2020), I'd been wrangling with a craving for an open-world driving game. Now at first, I figured it'd be fairly straightforward: a rigidbody, 4 wheels, do some torque & done!
Aaaand it is...n't. But it is? Anyway vehicles are strange beasts to do "right", especially with Unity's mystical WheelCollider
components, as it feels like half of their behaviour just isn't known anymore, let alone be easily understandable.
very TL; DR: when making a vehicle, be prepared to do many iterations of your wheel layout, your physics values (rigidbody, torques, suspensions), & also your controller class. Try to find a reference real-world vehicle for it's mass, it's torque, & shape, then start constructing your vehicle with those in mind.
So, from being a couple months into this project, here's my lessons learnt so far (bear in mind, this is for a slightly unrealistic Haul Truck/Buggy hybrid)
(For reference, here's the Wheel documentation link)
When you read the docs/manual, you'll see that unity tells you to make a list of a Wheel struct (which binds a wheel to some options, drive/steer being two of them). This may seem really obvious to some, but take that idea & continue with it. It'll save you a lot of headaches by splitting up your data/inputs/wheels code.
As of now, here's the structure I've refactored to:
turnModifier
- so each wheel can turn, but the back wheels use -1 to "anti-turn" and help steer the truck more.This structure has helped me a lot with working with the system as a whole:
At the moment, I'm also tinkering with having each wheel itself be responsible for taking Engine values then applying them. So the Wheels component just keeps each one updating in a single loop-per-frame, but the actual movement is kept local to the thing being moved. It's working decently so far (but alas I said that before I had to do a major refactor...)
A tip I only recently figured out: when using the Drive Torque, use a curve to have your torque falloff based on speed (e.g. at 0 speed, apply a full torque; at 30 speed, apply 0 torque).
Otherwise your wheels will literally just keep slowly speeding up until you hit some form of terminal velocity with your vehicle.
By using a curve you're more in control of the car as a developer, rather than it spinning out of control more easily.
(This may just be a basic gearing system, but my implementation is keeping it simple to Forwards/Neutral/Reverse)
At it's most basic, a Vehicle in unity is a Rigidbody, some colliders, and 3 or more wheels (trust me, I tried doing a bicycle once, and it was so much hard work - doable, but seriously hard)
To "init" one:
Now you should at least be able to move around. Now for the painful part: from here on it'll be a back-and-forth of balancing some key variables with your vehicle:
Finally, one trick I've been playing with is moving the wheel transforms, & modifying the wheel suspensions in runtime:
Those are some overall notes, but the gist is is that you you can mess with some of the WheelCollider settings as the player drive arounds; which can silently make your vehicle much much nicer to control (in my case, over undulating terrain, and with an unusual vehicle setup, every little helps)
There's probably some stuff I missed, but overall I hope this helps!