Vehicle Set Up
Last updated
Last updated
When initializing the controller, if you assign a mesh of the vehicle body, a center of mass will be placed in the middle of the mesh. Usually, that's good enough, but in reality, the center of mass would be a bit closer to the engine.
However, vehicles with the center of mass closer to the rear might handle worse, so you might want to set it closer to the front in all cases.
When initializing the controller, if the wheel transforms that you provide have MeshRenderer, a suspension will be placed above the wheel with the wheel radius * 0.125 vertical positive offset. That may or may not be ideal.
The suspension position defines the origin of raycasts and, if you enable "Restrain Wheels Position" on the CarVisualsEssentials script, the top point above which the wheel cannot go. If this point is too low, the wheel will go beyond the ground and the position won't be updated appropriately. Therefore, you might want to increase the vertical position of the suspension.
Select both gameobjects from the same axle and move them vertically as you like.
CustomVehicleController uses a number of raycasts with -wheel radius to +wheel radius offset for simulating suspension. You can change the number of raycasts on the CustomVehicleController component.
It is recommended to have at least 3 rays and the number of rays should be odd.
In case you are using multiple colliders or a mesh collider that the raycasts from suspension will go through, you need to set those colliders to a specific layer that will be ignored by suspension raycasts.
Next, select the same layer on the controller.
For correct wheel rotation, the center of geometry must be in the center of a wheel.
This can be changed in 3d modeling software. Or you can parent the wheel to an empty game object and set it's local position to be in the middle of a parent. If the controller is already initialized, you have to set a new reference to WheelVisualTransform of the VehicleAxle.
As for the steering (rotation around the Y axis), the wheel at a (0,0,0) rotation must have a forward direction same as the vehicle, or, in case the wheel must have a default rotation of (0, +-180, 0) to look rotated correctly, the forward direction must be opposite to the vehicle's forward vector.
During controller initialization, if the wheel transforms that you have provided do not have MeshRenderer that resembles the wheel, you'll also have to manually set the wheel radius.
Select the orthographic camera mode, focus on the wheel and change the WheelRadius property of the WheelController. Make sure Gizmos is enabled in the scene view, the sphere will be drawn to help you with setting the correct radius.
For correct collision detection at high speed, some changes to the rigidbody should be made.
Here are possible solutions:
Lower physics simulation fixed time step ([0.005 - 0.0166]) and keep rigidbody collision detection "Discrete". Pros: good at detecting all kinds of collisions and terrain angle changes. Cons: increased performance cost.
Set collision detection method to "Continuous" / "Continuous Dynamic". Pros: good at detecting collisions at high speed (dynamic also works for other vehicles). Cons: awful at detecting terrain angle changes (try driving on a ramp with a big angle (~20 degrees) at high speed).
Also, you may want to set rigidbody to interpolate or extrapolate position to fix vehicle jittering. It is a purely visual setting.