In my last post I explained how to attach an FMOD_StudioEventEmitter component to a game object, then, play it by adding a short line of code to the relevant script. Another method of playing FMOD events is attaching them to animations, this allows for much greater control with the ability to trigger sounds at any point along an animations timeline.
To do this I used a short script which calls an FMOD event from the FMODAssets project folder.
using UnityEngine;
using System.Collections;
public class SCRIPTNAME : MonoBehaviour {
public void EVENT NAME ()
{
FMOD_StudioSystem.instance.PlayOneShot(“event:/EVENTPATH“, transform.position);
}
}
Change SCRIPTNAME EVENTNAME and EVENTPATH to what is relevant to your project. The SCRIPTNAME can be anything you would like it to be, I usually prefix mine with SFX_ so they can easily be seen. The EVENTNAME is the name of the FMOD asset e.g. “Box_Break”, and the EVENTPATH can be found by clicking on the FMOD asset in the project folder within Unity and is shown in the inspector.
I then added this script to the root of the relevant GameObject in the scene. Depending on the game, there will be times when you will have to add this script to the prefab instead of the object in scene.
To trigger this using an animation, I opened the animation window and selected the relevant one. I then added my script to this animation by clicking the small white pencil looking button, selecting my FMOD script and dropping it into the timeline shown on the right. When this animation is then played in game, the FMOD event will be played every time the playhead reaches it’s point in the timeline.
Using this method gives you the ability to drop several FMOD events along an animations timeline and create flexible sound on complex objects.
Resources: