This is the final part of our Unity+FMOD+GVR tutorial series and we are covering the implementation into Unity. The videos are separated into two parts, implementing ambience and implementing the gunshot interaction. We hope these tutorials are helpful for you and please contact us at [email protected] if you have questions or comments. Note that GVR has been rebranded by Google as Resonance Audio. Thanks for watching!
If you missed the first two articles in this series, you can find them here:
In the previous video we set up an FMOD project with the GVR plugins. In this video we are going to set up the Unity project for VR and implement the ambience events. I’m building this using a Vive, but GVR and FMOD will work with any VR device and since the SteamVR prefabs use OpenVR, you can use any headset that supports OpenVR, including an Oculus.
Download the FMOD and Unity Projects
You can download the completed Unity Project and FMOD project used in these tutorials here:
If you are starting from scratch, the first thing we need to do is download the viking village project and the steamVR prefabs (If you are using the finished project this is already done.) Go to the Unity Asset Store and download them. Search “steamvr” for the steam VR package, download and import it. Then search “viking unity” and download and import the viking village project from Unity.
If the API Updater give you a warning, click “I made a back up, go ahead” (the version on the store is the backup). When the project loads Steam VR will pop up with a window select “accept all,” this ensures that the project is set up and optimized for VR.
Next, you will need the FMOD studio Unity integration. www.fmod.com/downloads. You will need the same version of the Unity integration as the version of FMOD studio you are using. The integration includes the necessary GVR assets that FMOD needs. Import it by going to Assets->Import Package->Custom Package… and browse to where you downloaded the integration. Then go to FMOD->Event Settings and in project path, and browse to your FMOD project. Also, in the Event settings, scroll down to plugins and add “gvraudio”.
Editing the Level
Open the viking village scene in Assets-> Scenes.
If you start seeing warnings relating to lighting in your console. Go to Window->lighting->settings-> and click generate. You will see a blue bar in the bottom corner of the screen showing the progress of the light maps. It is ok to continue working on the level as this is happening. (I didn’t in the video because I was screen recording and didn’t want to overuse the GPU).
I’m going to enable world boundaries, delete the canvas, event system, mouse lock and quality manager, this holds the previous player controller. Next I’m going to import the SteamVR prefab in SteamVR-> prefabs. Import the steamvr and the camera rig prefabs. The camera rig is a basic setup that links into the Vive that we are going to extend for our purposes.
Next up, we need to specify our FMOD listener go into the hierarchy and open up the camera rig and camera(head) and open the ears in the inspector. Disable the unity audio listener and click “add component” and type in FMOD listener, then select the script.
Adding the Ambience Objects
Now we are going to add our ambience. Right click in the hierarchy and create an empty gameobject, name it ambience, and add the FMOD studio event emitter, set the play and stop to object start and destroy, respectively, and then select your event from the FMOD event browser.
Now lets test our level! Hit the play button, and we should be able to turn and hear the soundfield changing. Its important to note that it doesn’t matter where in the scene this soundfield is because sound fields do not take into account location; only rotation.
We want to set up some more sources so that the ambience changes as we move throughout the level. So lets add another object and add the FMOD emitter for the ocean emitters. Again set the play and stop to start and destroy, then add our event. On this one it is important to place it accurately, so I’m going to put it below the listener and close to the shore. Next, we’ll make a prefab out of it by right clicking in the project window and right-click->create->prefab, then drag the object from the hierarchy to the new prefab. Now drag the new prefab into the scene line the shore with a few more emitters. I’m going to add a new empty game object and place all of the water sounds in there, just to keep my hierarchy cleaner.
There are quite a few torches throughout the level that we are going to add sounds to, luckily they are prefabs, so we only need to edit one and then apply the changes to all of them. Select one of the torches and go the the prop_torch_01 object and add an FMOD emitter and again link the event and start destroy for start and stop.
Its important in vr to be very precise as to where you are placing your sound emitters. In 3d games everything is playing on a single plane, so if we were designing a car the wheels driving over dirt and the engine could come from the same source and sound fine, but in vr the wheels need a separate source from the engine because in reality those 2 sounds are coming from 2 different places in space. Likewise with a giant monster – in vr you shouldn’t place the foot falls at the same place the roar comes from.
Back to the torch, once you have the source placed on the torch you can go to the pf_torch_stick – the top level of the prefab and hit apply. It will add our changes to every prefab of this object – it will apply ALL changes except for the position and rotation of the transform so be careful when editing. There are 2 other torches in the level that weren’t updated, select one of them and add the event.
We also have a sound for the giant cranes. I’m going to add this event to the wheel object and the sound will come from the center of the wheel. Again update the prefab and it will add our sound to the other 2 cranes.
I’m also going to add a few frogs in the grass to the level, make a new empty gameobject and add the fmod emitter link up the sound. I’m going to duplicate this object (ctrl+d) and change it to the other frog sound and move it to another patch of grass. I’m not creating a prefab of this because I only want one of each, but if I wanted more I would make a prefab for both.
The last piece of ambience we have is a bird. We’re going to animate the position of the bird so it is flying around the area. Create a cube, and I’m going to put mine on the wall to start. Add component-> animator and add the FMOD emitter. In our project create an animator controller, name it bird and link it to our bird object. Open the animator window right click and add an event. Then in the project add an animation, link it to the event and make sure to set it to loop in the inspector. Open the animation window and select the bird. In the animation window add property->transform->position. I’m going to make my animation 1 minute, so scroll wheel to zoom out and move the end keyframe to 60:00. Move the time bar and then set the position. I’m going to have the bird fly around our head.
Save the scene and test it out. Be sure to turn off the record mode in the animation window.
For the last part of this video, we’re going to add a way to move through the level. On the camera rig, I’m going to put it on the left controller, go to the controller left object – it should have the steamvr_tracked object on it already and add 2 components, the steam teleporter script and and steam laser pointer. Change the color to red and the teleport on click to true.
Part 2 – Adding the Gun Interaction
In order to add the gun interaction we need to get a gun model. Go to the Asset store, and to get the one I used, search “gun 1911”. Download and import the package. Then, add one of the models in the package the right controller and adjust the transform, so it is accurate to the controller. Then turn off the controller.
Add a line renderer, uncheck world space, set the width to .1, and the positions to (0, 0.7, 0) and 0, 0.7, 100). This will create a line from the gun barrel to 100 meters in from the of the gun barrel. In order to get the line to be red, we will create a new Material. In the project right-click->create->Material and set the albedo to red and emission to red. Add the material to the renderer.
Coding the Gun
Create a new C# script, I called it Controller_Gun and attach it to the right controller object. In your code editor (I used Visual Studio) delete the start and update functions and remove the Monobehaviour parent class. Replace it with SteamVR_TrackedController. We want to extend the OnTriggerClicked function to do something when the controller trigger is pressed. In this script we will also need to add an FMOD event reference and play a oneshot in the OnTriggerClicked function.
Back in the Inspector, link up the FMOD fire event. save and test it out.
We are going to add another script, called Target, that will be placed on everything that can be shot with the gun. This one will be a MonoBehaviour, be again we don’t need Update or Start. Add an FMOD Event Ref and create a new function called PlayHitSound that takes a vector3 and it will play an FMOD OneShot at the given location. The objects that will have this script will need a collider and the appropriate FMOD event linked to the script. I started with the terrain_near (it has a mesh collider) and linked the dirt hit event from FMOD.
Back in the Controller_Gun script we need to add the interaction by using a ray cast and get data from what we hit. If the object we hit has a Target script, we call the PlayHitSound script on it to play the appropriate hit sound.
Make sure to save and test at this point.
Making the environment responsive
The next part consists of going through each object you want to interact with the gun and making sure it has a collider and a Target script with the appropriate sound. Much of the level has been made with prefabs and is easy to add to one and apply changes to all. You can also multi-select and add component to add the same component to all selected items. (this is already done in the completed project available for download above)
Adding GVR Audio Rooms
The last thing we are going to cover is adding reverb zones. I added 2 to the level: one in the big storage area and one in the blacksmith. In order to do this add a new empty child object to the building you want to have reverb and add the fmod gvr audio room component, then approximate the size of the room. Any sound emitted in this zone will have reverb applied based on the parameters set in the inspector. In Unity front wall is positive Z, back wall is negative Z, left wall is negative X, right wall is positive X, ceiling is positive Y and floor is negative Y.
Thats it, you now have a interactive VR level! Thanks for watching! again, if you have questions or comments contact us at [email protected].