How We Implemented Vehicle Mechanics in Our Game — Part 5

Otto Wretling
4 min readJan 3, 2022

This article is about the development process of our first game. During the process, we are learning how to use Unity & Blender from scratch to create everything you see in the article, enjoy!

Hello and welcome to the series of articles detailing how we are developing our first game! We are currently in the process of implementing everything to make vehicles work in the game. In the last article we implemented camera changes when using vehicles, and this time we are going to work with implementing basic collisions.

When starting out with this part, there were no colliders implemented and vehicles were completely able to float through houses and other objects.

Prototyping in the Unity Editor

Pictured; lots and lots of colliders!

As I usually do, I started with implementing changes in the Unity editor first before diving into the code.

I added colliders to all of the objects in the scene that should be able to collide with vehicles(see the image above). Next, I added a box collider to the board that the player is standing on when using the vehicle and unchecked the checkbox Is Trigger on the box collider component, which activates collisions with other objects. Unfortunately, this did not go as well as I had thought;

To solve the problem, I tried to raise the vehicle from the ground to see if it would stop colliding in mid-air and to find out if the issue is caused by the vehicle colliding with the ground.

Raising it from the ground did not solve the problem, and the vehicle still rotates and collides with something. My next theory is that the player's Rigidbody component is colliding with the collider of the vehicle, so I’ll try to disable the Rigidbody to see what will happen.

As you can see, the vehicle no longer collides with itself and now we know that the Rigidbody was the problem and what to implement in code in the next section;

When Getting on the Vehicle

  1. Disable the Rigidbody of the player
  2. Set “Is Trigger” to false for the box collider of the vehicle

When Getting off the Vehicle

  1. Set “Is Trigger” to true for the box collider of the vehicle
  2. Enable the Rigidbody of the player

Implementing the Changes in Code

First of all, let's add a new function in the vehicle script called useVehicleCollisionsOnly that takes in a boolean parameter linked to whether the player is using the vehicle. The second parameter that the function uses is for knowing which GameObject represents the player.

public void UseVehicleCollisionsOnly(bool mode, GameObject player)
{
if (mode){}
else{}
}

As a first step, let’s link the mode variable to whether the player is using vehicles and enable or disable the Rigidbody based on that;

player.GetComponent<Rigidbody>().detectCollisions = !mode;

In the current version of the vehicle that we have, the board that the player character stands on when using the vehicle will be the part of the vehicle that detects and reacts to collisions.

This part ☝

To be able to switch collisions on and off for the vehicle, I’m going to store the aforementioned game object as a variable that the function we just created can use and I will do it with the following variable declaration(I will use SerializeField with the variable so that I can assign the value in the Unity editor).

[SerializeField] private GameObject collidingObject;

Then I’ll add the following line to the function to enable or disable the box collider collisions for the board of the vehicle.

collidingObject.GetComponent<BoxCollider>().isTrigger = !mode;

Finally, I’ll add the following line to the function for getting in or out of the vehicle to start using the functionality that I have written in this article:

UseVehicleCollisionsOnly(player.GetComponent<MoveCharacter>().isUsingVehicle,player);

Okay, let’s try it out!

It works as intended and the player collides correctly when walking and using a vehicle.

This is the final working version of the function:

Conclusion

Since we now have working collisions for vehicles, this marks the end of part 5.

We are almost done with implementing vehicles but we have some things left to do, here is the current list of things to do before vehicles are fully implemented in the first version:

  • Implement specific animations for using vehicles
  • Implement a steering system where speed and direction are influenced by the wind rather than controlled directly by the player

See you in part 6!

--

--

Otto Wretling

Writing about my podcast, game development, technology, language learning, and whatever else comes to my mind!