Play sound on megaphones in unity


(James) #1

Hi guys,
I’m new to fmod and Unity. Would like to know how to set 3D sound at random located e.g. 3 megaphones and then when the alarm is triggered to play an alarm sound? I have tried to read up on attributes3D though don’t get it. Any help will be appreciated.


(Nicholas Wilcox) #2

Once you have the (X, Y, Z) values in the Attributes3D object you would call EventInstance.Set3DAttributes


(James) #3

Thanks Nicholas Wilcox, this is kind of the solution I went with which worked. The only problem is that when the level restarted the alarm were playing in the background. Any ideas what I need to do.

using UnityEngine;
using System.Collections;
using FMOD.Studio;

public class TurnAlarmOn : MonoBehaviour
{
private AlarmLight alarm;
private PlayerHealth PlayerHealth;

//--define the event instance for the alarm sound
private EventInstance alarmRingEv;

//--define playback state for the alarm sound
private PLAYBACK_STATE playBack_stateAlarm;

//--define 3D attributes for 3D sound  to emit
private ATTRIBUTES_3D soundEmit;

//--define transform objects which this script will be attached to
public Transform sirenObj;


void Start()
{
	//--setup the reference to the alarm light.
	alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();

	//--obtain Vector3 coordinates for the megaphones located around the Stealth scene
	soundEmit.position.x = sirenObj.transform.position.x;
	soundEmit.position.y = sirenObj.transform.position.y;
	soundEmit.position.z = sirenObj.transform.position.z;

	//--lookup for the alarm sound file
	alarmRingEv = FMOD_StudioSystem.instance.GetEvent("event:/alarmSound");

	//--emit the alarm sound as 3D
	alarmRingEv.set3DAttributes(soundEmit);

	//--condition to stop the alarm if it is playing
	if(playBack_stateAlarm == PLAYBACK_STATE.STOPPED)
	{
		alarmRingEv.stop(STOP_MODE.IMMEDIATE);
		Debug.Log ("Stop alarm");
	}
}

void Update()
{
	alarmRingEv.getPlaybackState(out playBack_stateAlarm);

	//--condition to start the alarm if its not playing, if its has not stopped
	if(alarm.alarmOn && playBack_stateAlarm != PLAYBACK_STATE.PLAYING)
	{
		alarmRingEv.start();
		Debug.Log ("Start alarm");
	}
	else if(!alarm.alarmOn && playBack_stateAlarm != PLAYBACK_STATE.STOPPED)
	{
		alarmRingEv.stop(STOP_MODE.IMMEDIATE);
		Debug.Log ("Stop alarm");
	}
}

}


(Nicholas Wilcox) #4

GetEvent is returning you a new instance that is different to the one that was playing previously. I would recommend using the the Unity OnDisable or OnDestroy functions to stop the audio.