Creating a vehicle
Base hierarchy and components
Create an empty GameObject in the scene (ctrl-shift-N). Name it Vehicle. Add these components (from the Component menu):
- Component > Vehicle Physics > Vehicle Controller (a RigidBody is added automatically)
- Component > Vehicle Physics > Vehicle Input
Create a child GameObject (ctrl-alt-N). Name it WheelColliders.
Create four children GameObjects under WheelColliders. Name them WheelFL, WheelFR, WheelRL, WheelRR. Your vehicle GameObject should be like this:
Vehicle |- WheelColliders |- WheelFL |- WheelFR |- WheelRL |- WheelRR
Select the four WheelXX GameObjects, then add a VPWheelCollider component to them:
- Component > Vehicle Physics > Wheel Collider
Add the vehicle mesh as child of your Vehicle GameObject. The vehicle mesh should reside inside its own sub-hierarchy entirely:
Vehicle |- WheelColliders | |- WheelFL | |- WheelFR | |- WheelRL | |- WheelRR |- My3DVehicle | (hierarchy here is specific to each 3D vehicle) |- MeshBody |- MeshFrontLeft |- MeshFrontRight |- MeshRearLeft |- MeshRearRight |- ...
For each VPWheelCollider component (WheelXX) configure the property Visual objects > Wheel to their corresponding counterpart in the mesh. E.g. WheelFL > Visual objects > Wheel to MeshFrontLeft.
Select the four WheelXX components. Then click the context menu for the VPWheelCollider component and choose Adjust position and radius to the Wheel mesh. VPWheelColliders are automagically adjusted to fit the wheel meshes.
If you want / need to do it manually:
- The position of the VPWheelCollider component must be the same as the Transform for the corresponding visual wheel.
- Use the VPWheelCollider's center property for moving the wheel shape to the outer bound of the visual wheel.
- Adjust the radius property for matching the radius of the visual wheel.
Configure the axles property in VPVehicleController. Each axle must receive the references to their respective left-right VPWhelCollider components.
Add or configure the vehicle collider. The 3D vehicles usually come with a simplified shape of the vehicle (collider). This is used for collision detection. Add a MeshCollider component to the collider object and mark is as Convex.
If the vehicle comes without collider you can add a Cube as child of the Vehicle object, then scale it to roughly match the shape of the vehicle.
VPWheelCollider components must be located inside the vehicle's colliders
Ensure that the top half of each VPWheelCollider component is positioned inside regular convex colliders belonging to the vehicle's main rigidbody. No joint-attached bodies and colliders are valid here. Otherwise, strange behaviors and side effects may happen when the vehicle makes contact with other objects.
At least one convex collider is mandatory in the vehicle
A vehicle without colliders will exhibit a very weird and unnatural behavior as soon as the simulation starts, unless the inertia tensor is explicitly set (Rigidbody.inertiaTensor). Also, as described above, bad things will happen when contacting other objects.
First test drive
Let's check out if everything's ok so far. Configure these components as follows:
- mass = 1000
- drag = 0
- angular drag = 0
- Interpolate: Interpolate.
- Telemetry (optional)
Add this component to the vehicle (Component > Vehicle Physics > Telemetry). It will show us what's happening under the hood:
- Show Data = enabled
Click Play. The vehicle is now live in the scene and you can drive it around using the standard keys (arrows or WSAD). Wheels should move and steer properly.
You can now head to the Vehicle Setup Guide for configuring the vehicle dynamics (engine, brakes, etc), or continue here for adding further components and features such as audio and visual effects.