How We Implemented Vehicle Mechanics in Our Game — Part 3
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!
Introduction
In the previous article, I implemented functionality for getting into and out of the vehicle and disabling the walk animation when using the vehicle. In this article, I will mainly fix some bugs and unwanted behavior before we continue implementing more cool features to the vehicle system.
Gravity Bug
The first thing I noticed after playtesting the game in its current state was that I missed implementing functionality in part 2 to stop gravity when using vehicles. This will not do since the vehicles are supposed to be completely airborne and transport the player across the floating islands of our game world.
To solve the issue, I wrapped the code in the movement script that handles gravity inside an if case
for the variable isUsingVehicle
;
if (!isUsingVehicle)
{
movementOutput += Physics.gravity;
movementOutput.y = ySpeed;
}
As you can see, the result was that I was able to fly the vehicle as I expected but the player was falling way too fast when getting off the vehicle.
This made me think of a similar issue that occurred when I implemented jumping, which was actually caused by the same bug;
It turns out that when you get on the vehicle, the player is technically not on the ground, and therefore the script adds downward velocity to the player. This would normally be fine but when the player is using a vehicle, its position is locked to the vehicle which means that the downward velocity just accumulates until you get off the vehicle and then you shoot downwards like a rocket. 🚀
I managed to solve the problem by adding the variable isUsingVehicle
to the if case
required to add downwards velocity to the player.
This means that the game should not be adding more gravity if the player is on the ground OR using the vehicle.
if (characterController.isGrounded || isUsingVehicle)
{
ySpeed = -0.5f;
...
}
The result turned out exactly as I wanted, the player now falls at the appropriate speed regardless if you use a vehicle or just jump off the island.
Disabling Items While Using Vehicles
The next problem to tackle is that the player can use items while using the vehicle. To avoid a lot of unwanted behavior in the game, this needs to be fixed.
To disable the use of items while using the vehicle, I tried wrapping the function for using items in the player's controller script inside an if case
for the same variable isUsingVehicle
from before.
if (!gameObject.GetComponent<MoveCharacter>().isUsingVehicle)
{
UseItems();
}
When I tried it out there was just one problem, I could no longer rotate the camera;
The solution for this problem appeared to be quite easy, all I had to do was to move functionality for rotating the camera outside the aforementioned if case
;
if (Input.GetMouseButton(1))
{
cam.GetComponent<CameraMovement>().allowRotation = true;
}if (Input.GetMouseButtonUp(1))
{
cam.GetComponent<CameraMovement>().allowRotation = false;
}
Let's go back to the issue of being able to interact with items while using the vehicle and see if I managed to solve that problem.
When I looked at this issue and playtested it, I also detected another bug that affected the items;
Take a look at the list of colliding items to the right; it keeps track of which items can currently be interacted with. When I drive the vehicle close to items, they get added to the list and removed when you go away, the same goes for walking.
The bug appears when you park a vehicle near an item. If the vehicle is still colliding with one or more items, they are not removed from the list of colliding items and therefore it is possible to pick up a watering can from 10 meters away as you see at the end of the GIF.
The Solution
By wrapping the function that keeps track of the list of colliding items inside an if case
for the variable isUsingVehicle
from before, I managed to solve the problem! This also accidentally implemented functionality that disabled the use of items while using a vehicle.
private void OnTriggerEnter(Collider other)
{
if (!(gameObject.GetComponent<MoveCharacter>().isUsingVehicle))
{
...
}
}
Conclusion
Welcome to the end of part 3, we have now fixed some issues and are ready to continue this journey in the next part.
The list of things to implement for vehicles is getting shorter and shorter, but we still have some ways to go yet. These are the features that remain to be implemented before the vehicle system is ready:
- Automatically move the camera further away from the player when using a vehicle
- Implement proper colliders for vehicles, so that they can bump into things and react somewhat realistically and use some kind of buoyancy system
- Implement a specific animation for using vehicles
- Implement a proper steering system where speed and direction are influenced by the wind rather than controlled directly.
See you in part 4!