Please notice there are two versions of script for Vehicles: the old ones are the scripts like "car physics", "car vehicle", which are obsolete; the new vesion is one script called "Modular Vehicle", it combines all kinds of vehicle together(land, hover, boats) so you can adjust this one script for all types of vehicle settings. The new version resolved many bugs of the old version, so we recommand to use the new one. We will still keep the tutorial of old version in other pages, you can always check them if you need.
Note: This page is still updating so there may be some place incomplete during this moment.
To start create a land vehicle, you should find yourself a vehicle model.
To prepare a model for vehicle, whether you want create one using MAYA or 3DMAX or Blender, or find a model from Unity asset store, there are somethings you should pay attention to:
Make sure the wheels are splited from the vehicle body. Some vehicle in asset store has a single FBX file combined all the things together, which makes the next steps quite impossible if we don't do something to the model.
You can always choose a vehicle more realistic and complex, but there may be more details need to adjust on your own.
Create and organize the objects for the vehicle like this:
Carbody is the body of the car, not include the wheels.
Seat is an empty object here, it defines where your avatar sit in the car.
The wheel group contains the 4 wheels, and the wheel collider group contains the wheel colliders for each wheel.
The positions of the wheel colliders need to adjust individually, so they should be created seperately from the wheels. You can do it by creating an empty gameobject then add a component of wheel collider, duplicate 3 more, rename them.
Choose the parent object of the whole car, then add a script named "Modular Vehicle". A rigidbody component will be added automatically as well.
As a simple guide, it will only show you how to make a car which can be driven in the game. There's a few more stuffs in the script settings but we will ignore them here.
Set the "Seat Root" with the seat gameobject, and Ride Animation you can get it from Mixamo freely.
The Mass will be the weight of your car, the value here only shows an example.
Note: there is also a Mass property in Rigidbody component, change it to the same value.
The Center of gravity concerns the stability of the vehicle. If you want to create a big truck, make it a little lower may be better(like (0, -1.25, 0)), otherwise just leave it to default.
These are the 'core forces' settings. They configure the vehicle 'overall'. Everything else is pretty much 'per wheel'.
Our example is a regular 4 wheels car, so we set the number of wheels as 4. If you are creating a vehicle with different number of wheels, just enter the wheel number here -- and make the same number of wheel colliders.
This is a representative front wheel. The other uses the same values with its own transform and collider.
This is a representative rear wheel. Same for the values, it's just on the other side.
In this example, the front and rear wheel is in different size(it's a race car, somehow), so the mass and radius are different. For a normal car, they might be the same value.
This is the collider for the rear wheel. It will be the same on the other side. Differences are in the name and position on the model. This applies generally to all the wheel elements.
This is the collider for the front wheel.
After set the values, you should adjust its position and size manually. Make the radius match the size of wheel then modify the collider's postion to the center of the tire. Like this:
If you want to add tire track, here's a image from the old version, but it's always the same way.
Finally, add a box collider in the parent object, make sure it doesn't have conflict with the wheel colliders.
Forget this will make your vehicle cannot trigger the clickable events(which equals cannot drive it).
Now you can add a virtual good component to the parent object to upload and test it.
The hover points and boat forces are for hover type and boat type, which is still under testing now.
The example below is only a prototype, it can float and move, but the performance is not very satisfied. The value settings may be complex here, which may need you to adjust by yourself. Feel free to update this part if you find a more simple way to create a hover car.
Same as the land type, we found another model from asset store, and just turn the tires 90 degree, like this:
There won't be other collider, so a box collider covers the whole car body is enough.
Its parts could be organized like this:
The four wheels here will be the 4 hover points, which will be defined later.
Like create a land type vehicle, just add a modular vehicle to the parent object.
It seems there is a little bug that the mass in modular vehicle doesn't override the mass of rigidbody, so we use the mass value in rigidbody instead.
These basic settings are same as the land type.
Core force settings:
These values are experimental. Feel free to update it if you find a better example.
Here need some physical tricks and calculations. The Unity physic system defines the Gravitational acceleration is 9.81 m/s², we set the rigidbody mass is 100(the unit is kg, normally), so the weight is 9.81 * 100 = 981 N. We have 4 hover points, so we seprate a up force of 981N to these 4 hover points, that is, each hover point take a 245.25N in up direction. This should keep the hover car float in the air. Of course, if you want to increase or decrease the number of hover points, you shoud modify the force each hover point too.
Add the virtual good component and upload it, now you can test it on the preview server.
If you want to add some additional functions to your vehicle, like the speed meter and rear-view camera which introduced below, please make sure that you've add the events control to them. For example if you want the function only active when the player drive the car and deactivate them when the player leave, you need to add events in the On Player Activate/Deactivate function block, like the image below:
There is already a script called "speedometer" in the Editor Pack, all you need is create a UI Text Component under your vehicle object, and another empty game object with this speedometer component. Attach the rigidbody of the vehicle and the UI Text component to the slots of the speedometer, you can leave the rest option as it was, of fill it on your own request. Change the position and style of the UI Text as a normal UI element as you wish. And, remember, disabled the Canvas object at the beginning, and activate it by the events function as I present above.
A simple example here, made a meter with blue text in the right-bottom corner: