Sinespace
Ask or search…
K
Links

Vehicles

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.

Contents

Simple guide for a land vehicle

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.
As an example, we choose a simple low-poly car in the asset store(which the server can proceed faster) like this:
You can always choose a vehicle more realistic and complex, but there may be more details need to adjust on your own.
Preparation: Create and organise the objects for the vehicle like this: Carbody is the body of the car, not including the wheels. Seat is an empty object here, and it defines where your avatar sits 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 separately from the wheels. You can do it by creating an empty gameobject then adding a wheel collider component, duplicating 3 more, and renaming 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 are 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, making 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-wheel car, so we set the number of wheels as 4. If you are creating a vehicle with a 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.

Hover

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. Hover points:
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.

Boat

PENDING

Events

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 for 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:

Speed meter

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, or 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, is a meter with blue text in the right-bottom corner:

Custom Camera

Sometimes you may want set the camera for vehicle by yourself, or have several cameras to switch(you may need add custom script by using scripting runtime for the switch function though), like far view behind, close view behind, driver view, etc. To do this, you need to use the plug-in which is already contained in the Editor pack: Cinemachine Virtual Camera. Simply create an empty game object in the vehicle object, and add the component Cinemachine Virtual Camera to it. Drag it to the Primary camera slot in Modular Vehicle, just like this:
If you want to add a second or more camera, just set the number of the Secondary cameras then drag the cameras to each slot, like the example above. Don't forget to adjust the parameter of your virtual camera or it won't work, here is an example:
For the details of how to adjust the virtual camera and how to set those values here, check this tutorial video. And you also need to disable all of the Cinemachine Virtual Camera at the beginning, or it will bind your camera to them as soon as your vehicle is spawned.

Rear View

You may also want add a rear view to your vehicle, so the driver can take a clear look of his behind. The method is simple: First, in your assets folder, right click the mouse and create a new render texture.
Leave the settings as its default if you don't have any other request. Second, create a new camera in the prefab. The normal camera, not the virtual one we used above. Then rotate it horizontally, make it face to the behind of the vehicle, and move it to the end of the car, make sure there is nothing block the camera. Drag the render texture we create to the Target texture slot. And disable the camera component.
Now in the canvas we used to show the speed meter, add a raw Image and attach that render texture into the slot:
And Finally in-game, we can get a simple rear view of the behind:
You can use image mask to add a form of rear mirror to make it look beautiful, for more information, take a look at this Unity Beginner Tutorial.

Tutorial for obsolete vehicle script

Sinespace® is a registered trademark of Sine Wave Entertainment Ltd, All Rights Reserved.