How We Implemented Vehicle Mechanics in Our Game — Part 7


Welcome back!

We are currently focusing on implementing wind for our vehicle system. Since it was a while since the last article, let me give you a quick recap of the basic premise;


To make the sailing in our game more interesting, we want the wind to constantly be varying in intensity, a little bit like how the wind is unpredictable in real life.

To accomplish this, I have split the wind strength into two categories; major and minor wind forces. The major wind force can change the wind strength anywhere from no wind at all, to a light breeze, to somewhat windy, to a full-on storm, etc., and will change very slowly.

The minor wind forces on the other hand will determine the momentary changes within the constraints of the major wind forces as opposed to the major wind forces that may change from one day to the next.

After implementing the “major wind forces” in the last article, this time I will focus on the “minor wind forces”.

I will also try to define the “window” of the minor wind forces. This so-called window will be a maximum and a minimum of how far the minor wind forces can move from the value of the major wind force.

The (Wind)ow

Enough with that wall of text, let’s finally get to implementing code instead!
The first thing I will do is define the following two variables;

Once those are in place, I added a couple of lines to ChangeCurrentWindStrength() to calculate the size of the minor wind force window.

When testing the code, it seems that the window’s span unintentionally becomes smaller than 20 when the wind strength is within 20 units from either 0 or 100.

After thinking about the issue, I tried to solve it with some “spaghetti-esque” if/else logic:

…and it does indeed look like I got it working as I wanted to:

Now that the functionality is working as intended, I’ll make sure to refactor the code into a more readable function.

When a variable has to be defined based on a true or false value, I prefer to use the ternary operator to do so because it is shorter and becomes more readable.

If you don’t know what the ternary operator is, you can watch this great video from Unity that explains it:

Here are the expressions after I refactored them to use ternary operators instead.

So the next step of the implementation will be to create a new variable for the actual value of the minor wind force, but before I do anything else, I decided to rename the variable strength into baseStrength, to make it more clear what information it holds.

After that, I’m going to create a similar variable to baseStrength and have it increase and decrease randomly between minorWindForcesMinimumStrength and minorWindForcesMaximumStrength at a faster rate than baseStrength.

I will initialize the new variable as flexibleStrength and put it directly after baseStrength.

As I did with baseStrength, I will also add code to set a random value for flexibleStrength in Start().

After testing it out, it seems to set the value as expected!

The next step will be to make flexibleStrength swing back and forth between minorWindForcesMinimumStrength and minorWindForcesMaximumStrength and finally then set the sailing speed to that value.

I’ll start by adding this line to make flexibleStrength change dynamically:

Note that this time I’m using 10 instead of 1 in Random.Range() to make the minor wind force change faster.

As a last change, I decided to rename flexibleStrength to outputStrength just to make it more clear for posterity what is being stored in the variable.


Thank you for reading all the way through the article!
So far we have the wind strength fully implemented, and next time we will connect the wind strength with the speed of vehicles.

See You Next Time!



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!