Truck + Trailer Implementation

Truck Implementation

The truck implementation is identical to the vehicle implementation. See Vehicle Implementation

Ensure that the truck functions correctly within your scene by testing it before proceeding to implement the trailer.

Trailer Implementation

The truck should look like this before starting to add the trailer:

Follow these steps to implement the trailer properly:

1. Add the trailer attachment point.

Create an empty GameObject and position it precisely on the attachment point. This represents the point where a joint will be created.

The trailer has a similar point and the 2 points should have the same position.

2. Add trailer prefab inside & attach TrailerComponent.cs to it

A Rigidbody will be automatically created for the trailer.

Ensure that the mass, drag, and angular drag values of the trailer match those of the truck for optimal performance.

It is not essential to position the trailer correctly inside the prefab initially, as it will be automatically placed in the correct position later on.

3. Add the TrailerHolder

Create an empty GameObject, position it as the first child, set its position to (0,0,0), and then place all other GameObjects from the trailer prefab inside it.

This is necessary to automatically position the center of mass and configure other internal settings.

4. Add the truck attachment point.

Create an empty GameObject and position it where you intend the trailer to connect to the truck. During runtime, the attachment points of the truck and the trailer will align, so it's important to ensure their positions are precisely set on top of each other. Pay close attention to their positions.

5. Assign the trailer attachment point.

Assign the anchor point to the TrailerComponent script.

6. Add trailer collider.

Add one or more colliders to the trailer, depending on its geometry.

7. Configure the wheel pivots

The wheels of the trailer should be configured as any vehicle wheels.

The first pivot should be on the ground as shown here:

The second pivot should be in the center of the wheel. The difference between the first pivot and the second pivot represents the wheel radius.

Inside the second pivot, you should place your wheel graphic with the pivot in the center of it.

8. Automatically configure

After all the above setup is done, press the Configure Trailer button.

If everything is successful the following message will appear inside the console:

This will automatically create a configurable joint and assign all the necessary references.

By default, standard settings will be applied. However, if you prefer custom settings, you can configure the joint from the trailer according to your preferences.

If something is not properly configured, you will get a message inside your console informing you that something went wrong.

9. Add the trailer component to the truck

On the VehicleComponent from the truck add the trailer and the Trailer connector:

Afterward, click the 'Configure Car' button, and the trailer should snap into the correct position. Additionally, the Rigidbody from the joint should be assigned.

Don't forget to set the Layer of the prefab and all its children to the Traffic Layer to work properly

Last updated