How We Implemented Vehicle Mechanics in Our Game — Part 2

Otto Wretling
4 min readNov 14, 2021

--

Up, up, and away!

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

After sidetracking a little bit and implementing jumping last time, now is the time to continue with implementing vehicle mechanics. In this article, I will try to implement the ideas from the previous article into code and switch the walk animation on and off when the player gets in or out of a vehicle.

Setting It Up in Unity

This time we have created a new vehicle model that we will try implementing the mechanics with. The model represents a balloon at the top that lifts the player and something that resembles a windsurfing board that the player character will hold on to and steer the vehicle with the help of the wind.

To ensure that the game can detect when the player is close enough to climb aboard, I started by adding a box around the vehicle. I also added an entry point and exit point on the vehicle that represents the player's location when entering and exiting the vehicle.

The box collider around the vehicle to the left, and the right, the entry and exit points for the vehicle.

Getting Into the Vehicle

I started by creating a public variable called isUsingVehicle in my movement script that will keep tabs on whether the player is using the vehicle or not.

The variable is used in the vehicle script and the function is called if you press the E key on the keyboard while the vehicle is within range.

After trying it out, it seemed to work as I expected it to. The player teleported to the correct locations but looked in the wrong direction when entering the vehicle.

To fix this issue, I changed the reference player.transform.position and used the built-in function player.transform.SetPositionAndRotation instead to set both the rotation and position at the same time when entering the vehicle;

player.transform.SetPositionAndRotation(cockpit.position, cockpit.rotation);
Luckily, this worked on the first try!

The next problem that I needed to solve was that the player rotation was not consistent if you tried to enter the vehicle while holding W or some other movement key; the player character just kept going instead.

I had to make sure that the player character was attached to the vehicle while it was being used.

I solved the problem by making the vehicle into a child object of the player character while the player was using the vehicle. In programmatic terms, I did this by adding transform.parent = player.transform; when getting on the vehicle and transform.parent = null; when getting off.

As you may have noticed in the GIF above, the vehicle was also teleported to the exit point when getting out of the vehicle. I managed to fix the issue by removing the player as the parent of the vehicle before I teleported the player to the exit location.

Hurray! Another bug bites the dust!

Stopping the Walk Animation

At this point when using the vehicle, the player was still playing the walk animation and the next step was to stop that from happening.

I wrapped the function for the walk animation inside anif casefor the variable isUsingVehicle that we created earlier.

When I initially tried it out, the animation did stop as I expected, but if I kept holding one of the WSAD keys while getting on the vehicle, the walk animation was still playing and would keep going even after I stopped moving.

To solve it, I added the line animator.SetInteger("AnimPar", 0); to the animation script. This line made sure that the walk animation would always stop whenever the player was using the vehicle;

Conclusion

This is it for part 2, we have come far enough to be able to enter and exit vehicles with a press of a button, and stop the walk animation automatically in preparation for animation more suited to using vehicles.

There is still a lot to implement when it comes to the vehicle system, but I hope to see you in the next article where I try my hand at one of the following features:

  • Automatically move the camera further away from the player when using a vehicle
  • Disable interactions with items 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

--

--

Otto Wretling
Otto Wretling

Written by Otto Wretling

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