Things you should know before making a Vehicle in Unity


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)


Your Code

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:

  • ScriptableObjects for your "engine" values (Drive Torque, Brake Torque, etc.). This makes them easily fetchable via variables on your other classes, and helps to make it easier to edit values & preserve them in/out of play mode (you'll be doing a fair amount of tinkering)
  • For your wheels, make a class that wraps all your wheels with a struct. All this class should do is provide a single source for fetching & looping through all the wheels on your vehicle.
    • In here, you can take Unity's notes and run with it - for me, each entry in the Wheels struct also has turnModifier - so each wheel can turn, but the back wheels use -1 to "anti-turn" and help steer the truck more.
  • Finally, have a class that takes player Input, then processes that (with your Engine values Scr. Obj.). This then uses your wheels component to loop through (or vice versa, have your wheels script fetch current values from your "Engine" component)

This structure has helped me a lot with working with the system as a whole:

  1. It's easier to edit the car behaviour, it's all in the Engine (especially when it comes to handling Forwards/Neutral/Reverse)
  2. It's easier to add more variables into the system, that's nested in the assets.
  3. And finally, saves redundant looping

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...)


Driving your wheels

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)


Your Vehicle

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:

  • your rigidbody needs to use semi-realistic weight values, each unit of mass is a kilogram
  • your wheels will have tense springs, so go in there & increase their suspension distance a little
  • and then position your wheels such that their transform handles are around the bottom of your collider, and that they centre the collider's centre between them (think centre-of-volume, not the pivot)

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:

  • The mass of your vehicle
    • tuning it up/down by 100kg or 1000kg at a time changes things a lot
    • Also, absolutely experiment with manually setting the Centre of Mass position yourself - a lower CoM will stabilise your vehicle wonderfully
  • The layout of your wheel colliders
    • try out a few different layouts before you commit to a vehicle design
    • by which I mean - space them out horizontally, or make your back wheels more spaced out than your front wheels.
  • And for the wheels, I've focused on the "Suspension Spring" values - I've tried messing with Mass, but dunno, doesn't seem to do much?
    • Tweaking the suspension distance to give some more wiggle room (which stops a severe CLANG if a wheel runs out of distance, at the cost of more rocking/rolling whilst your wheels try to balance out the pitch)
    • Spring is how powerfully the spring will try to push back to the target position
    • Damper makes the spring looser
    • Target Position dictates whether the spring wants to return to the "top" of the suspension radius, or the "bottom" (edit it with the gizmos on in the scene view)
    • (I haven't touched the Friction values at all because I'm scared of them)

Tinker with your wheels in runtime

Finally, one trick I've been playing with is moving the wheel transforms, & modifying the wheel suspensions in runtime:

  • When driving forwards, stiffen your back wheels
  • When turning, stiffen & push away the opposite side of wheels - i.e. when turning right, make the left suspensions stronger, then push them away. This helps stabilise a top-heavy truck.
  • When braking, stiffen the front wheels a lot, loosen the back ones

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!

Get A Day of Maintenance (Jam Version)

Leave a comment

Log in with itch.io to leave a comment.