How We Implemented Vehicle Mechanics in Our Game — Part 1

Otto Wretling
5 min readNov 1, 2021

--

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!

This game will be a kind of farming simulator that takes place in a fictional world with floating islands. When it comes to transportation in the game, it would be pretty handy to be able to get around these islands conveniently. This is where the airship comes in; your ticket to anywhere!

She may not look like much, but she’s got it where it counts, kid. I’ve made a lot of special modifications myself!

We plan for the vehicle mechanics to work similarly to a sailing ship where you use the wind and sails to influence the speed and direction of the ship without being able to control it directly. We reason that this might add a little more challenge and fun to the otherwise mundane task of getting around.

Basics First

At the very beginning, the airship was just a model imported from Blender without any collider or code attached in Unity.

Every journey starts with one step… through the floor?

The first step was to add a collider to the deck of the ship so that you wouldn’t fall through it. I added a box collider to the deck of the ship, Unity seemed to place it correctly on the first try, and it worked perfectly when I tried it out!

There we go! First step aboard taken!

Prototype First, Implement Later

At this point, I decided that before diving headfirst into coding, I wanted to implement the driving mechanics in the Unity editor so that I would have a clear picture of what I needed to code and how to do it.

My initial theory was that it should be possible to steer the ship simply by making it a child object of the player object.

It may not look realistic, but it’s a start!

As you can see, I successfully managed to steer the ship, but since the ship was supposed to float in the air, I have to modify the physics from what the player currently uses when walking.

I turned off gravity in the movement script to see if that could solve the problem. I did this by commenting out the following line of code that I added when implementing physics.

movementOutput += Physics.gravity
Two steps forward, one step back

After turning off gravity, something else seemed to be very wrong; When trying to move, the ship floats around the player for some reason, which also made it impossible to steer the ship reliably.

It turned out that the ship's box collider that I added earlier was colliding with the player when the movement animation was running. The walking animation will be disabled and replaced with a more suitable animation at a later stage but either way, something had to be done to keep the ship from colliding with its passenger.

There we go, no more ship-related nausea

I turned off the collider for the ship and voilà; I could now steer the ship in the air as expected.

I realized that it might not be the best solution to turn off the ship's collider since I wanted the ship to bump into things when getting too close, so I chose to enable the colliders for the ship and disabled the collider for the player instead. This time I could steer the ship like before but I noticed a couple of things that had to be fixed;

This time, things seem to go wrong slowly instead of fast. Does that count as an improvement?

When bumping into another object, the ship seems to tilt slowly and keep going instead of getting back to its default position like a boat in the water would, something that has to be fixed.

End of Part 1

Needless to say, I am far from done with implementing the vehicle mechanics. For now, I have gotten as far as I needed to in the Unity editor and for the next steps, I will start coding, implementing features, and fixing the bugs.

I hope you look forward to part 2, where I will try my hand at the following steps:

  • Implement the driving mechanics from this article into code and enable jumping in and out of the ship
  • Disable the walking animation when the player enters a vehicle
  • Fix all of the colliders on the island so that the ship collides correctly
  • Make sure that the boat goes back to a default position after bumping into other objects, like a boat rocking in the water
  • The camera is too close to the player when navigating the ship, so it has to move further away when steering the ship
  • There is a blue arrow appearing halfway through the GIF above that indicates that an item can be picked up. I will disable these interactions when steering the ship so that the player cannot pick up items or do other things that shouldn’t be possible when steering a ship

--

--

Otto Wretling

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