TANKS! - Unity/FMOD Scripting Tutorial No. 2


Hey everyone! This tutorial demonstrates how to send parameters from Unity to FMOD Studio, allowing you to add a bit more variety and interactivity to your events, using the Unity Tanks! tutorial as an example. This tutorial assumes you already know what you’re doing with the content of the previous tutorial, so if you don’t know or haven’t worked through that yet, have a look at it here!

Let’s get down to business! All that we need to do within Unity to expand on our previous code, and send a parameter to FMOD is the addition of a single line. However, some setup is needed in FMOD before we start to ensure that the FMOD project is ready to receive a parameter.

Open up your Tanks! FMOD project, open your ‘Shot’ event, and click the little ‘plus’ at the top of the window, click ‘Add Parameter’, name this ‘ShotForce’, and set the range to between 15 and 30 (this will be explained later when we return to Unity). Now, with the ‘ShotForce’ tab still selected, start automating your event against the value of the ‘ShotForce’ parameter. This is done by right-clicking on any editable value in FMOD, and clicking ‘Add Automation’.

The simplest way to hear this working for the purposes of this tutorial would be to automate the master pitch of the whole event, so when a fully-charged shot is triggered, the pitch is high, and when a weak shot is triggered, the pitch is low. Again, if you don’t feel you know enough about FMOD yet to follow along properly, I’d direct you to Sally Kellaway’s FMOD tutorial series here!

Once you have some parameter automation in place, we’re ready to move back into Unity!

Open up the ‘TankShooting’ script again, and navigate to where we triggered the “Shot” event, in the ‘public void 'fire'' function at the bottom of the script. Between the instantiation of the event, and the setting of its 3DAttributes, the following line needs to be added;

Shot.setParameterValue("ShotForce", m_CurrentLaunchForce);

Breaking this new line down, we can see what is happening. We type ‘Shot.’ so the script knows which event we are wanting to work with, and we type ‘setParameterValue’ so the script knows that we want to change the value of a parameter in FMOD. Within the brackets that we open after ‘setParameterValue’, we need to tell Unity the name of the parameter in FMOD that we want to change. This needs to be exactly the same as what you typed in FMOD - computers only do what you tell them to! It also needs to be within quotation marks, as this is a string.

After the quotation marks have been closed, we type a comma and then point Unity to the value we want to send to FMOD. In the case of the "Shot” event and the “ShotForce” FMOD parameter, we want this to reflect the actual force of the shot in the game. This is why we set the range of the parameter in FMOD to 15-30, as looking at the top of the script we can see that the minimum launch force (public float m_MinLaunchForce = 15f;
) is 15, and the maximum launch force (public float m_MaxLaunchForce = 30f;). The force of the shell being launched will only ever be between these two values.

The value we want to send to FMOD is the value that the game launches the current shell at, however, which is found in the variable ‘m_CurrentLaunchForce;’, defined based off how long the player holds the fire button down for. The game knows that m_CurrentLaunchForce is a float value, so it can be entered as part of our ‘setParameterValue’ function. Make sure to end the line with a semicolon, as always.

The final script should look like this;

Now jump back into Unity and play the game. You should be able to hear the parameter automation you set in FMOD changing based on how long you have held down the fire key for.

Don't hesitate to contact me if you have any questions!