46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using FMOD.Studio;
|
|
using FMODUnity;
|
|
using UnityEngine;
|
|
|
|
public class AudioDropRigidBody : MonoBehaviour
|
|
{
|
|
[SerializeField] public EventReference clip;
|
|
|
|
private EventInstance instance;
|
|
private EventDescription eventDescription;
|
|
|
|
private void Awake()
|
|
{
|
|
eventDescription = RuntimeManager.GetEventDescription(clip);
|
|
eventDescription.loadSampleData();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if(eventDescription.isValid())
|
|
{
|
|
eventDescription.unloadSampleData();
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision other)
|
|
{
|
|
var magnitude = other.relativeVelocity.sqrMagnitude / 25f;
|
|
|
|
var volume = Mathf.Clamp01(magnitude);
|
|
if (volume < 0.01f) return;
|
|
|
|
var pos = other.contacts[0].point;
|
|
|
|
if(!instance.isValid())
|
|
{
|
|
eventDescription.createInstance(out instance);
|
|
RuntimeManager.AttachInstanceToGameObject(instance, transform);
|
|
}
|
|
|
|
instance.set3DAttributes(pos.To3DAttributes());
|
|
instance.setVolume(volume);
|
|
instance.start();
|
|
}
|
|
}
|