How We Implemented Vehicle Mechanics in Our Game — Part 6

Introduction

Welcome back!

For the next part of the vehicle system, we are going to implement a wind system that will influence the direction and speed of vehicles in the game.

The system will influence the vehicle speed depending on three factors;

  • How much the sail is hoisted
  • The strength of the wind
  • The alignment, or rotation of the sail compared to the wind direction

Before I started coding, I looked at Unity’s built-in functionality Wind Zones to see if that could be used for the wind system. Unfortunately, the feature only seems to be built with animating trees and particle effects in mind. Therefore, we decided to create our own wind manager from scratch instead.

Implementation

The first step was creating a new GameObject that will have the wind script attached.

Next up, let’s create a script attached to this GameObject simply called Wind.

In the script, I will start by creating a variable for the strength of the wind, along with variables for the minimum and maximum wind values;

After creating the variables, I will randomize the value of strength in the Start() method, and playtest to make sure it works as expected.

Now that strength starts at a random value, we also have to change the wind strength randomly over time to simulate an ever-changing wind.

Wind Forces

My idea for the wind variation is to have two forces that change the wind strength continuously; minor forces and major forces.

The minor forces will change the wind quickly(in a manner of minutes), but can only change the wind within a set window of the total wind strength(about 20% of the total wind strength span), additionally, the minimum and maximum values of the window should also change over time.

The major forces will change the wind anywhere from 0% to 100%, but its effects should take a lot longer to take their course, maybe even real-life hours. In this article, I will start by implementing the major wind forces first.

Major Wind Forces

First, I’ll start by creating 2 variables that will represent the random values of how much the wind can change per second:

Next, I’ll set up a function that will do the actual changes to the wind strength and add it to the update method.

Before I try randomizing the wind strength, I will try increasing it over time to see if that works as intended as a proof of concept;

It seems to work, and the value starts at a random place and goes up slowly. One thing that bothers me is that the wind strength is currently able to exceed 100, which it should not do. The next step will therefore be to make sure that the value cannot go outside the range of 0 to 100.

By using the built-in function Mathf.Clamp, I make sure that the variable cannot go outside of the range of 0 to 100.

Now that we can increase the value over time and keep the value within set boundaries, it’s time to randomize the value instead so that the wind can be unpredictable and make for more interesting gameplay.

I’m going to start by increasing or decreasing the wind in each frame by a value somewhere in the range of -1 to 1.

After playtesting a couple of time, it seems like the value only decreases over time, and never increase despite being random. How come?

To find the answer, let’s look a bit closer at the Random.Range function by only outputting the following to the console:

For some reason, the function either outputs 0 or -1, but never 1 or any other positive number. One thing that also stood out to me was that the function exclusively returned integers, and never floats.

After reading the official documentation, it became apparent that because I only passed integers to Random.Range, it only returned integers(and for some reason never returns 1). Let’s try passing floats to the function instead and see if that resolves the situation.

Let’s add the randomized float value to the strength variable and see if the wind also goes up and down randomly.

The new function does work as expected, but it feels a bit too stable, and too boring. For the sake of experimentation, let’s increase the variables to make them change more often.

After playing around with the numbers, I came to the conclusion that setting the random value to somewhere between -50 and 50 made the wind vary more quickly in a short time, and made it easier to determine if the randomized value worked as expected or not.

Regardless of current results, I am not setting any values in stone for now, since I believe that the optimal setting for these variables should be discovered through lots of playtesting to see what makes the game the most fun.

Conclusion

Now that we have completed the implementation of the major wind forces, that concludes the first part of the wind system and the 6th part of the vehicle system.

Join me next time, where I will continue with the minor wind forces that will control the rapid movement while the major wind forces control the overarching ones.

Thank You for Reading!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Otto Wretling

Otto Wretling

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