Reputation:
I have a weird problem. I want to trigger a audio file with a collision box. The Debug Log confirms that the audio source is triggered, but it would play the audio. What could possibly be the reason here?
I have attached an audio listener to my camera, and I attached an audio source to an object that is quipped with this code:
using UnityEngine;
[CreateAssetMenu(menuName = "ScriptableCookbook/Actions/PlayAudioAction")]
public class PlayAudioAction : ScriptableAction
{
[SerializeField] private AudioClip clipToPlay = null;
private AudioSource audioSource;
public override void PerformAction(GameObject obj)
{
audioSource = obj.AddComponent<AudioSource>();
audioSource.clip = clipToPlay;
audioSource.playOnAwake = false;
audioSource.spatialBlend = 1;
audioSource.Play();
}
public override void StopAction(GameObject gameObject)
{
if (audioSource != null)
{
audioSource.Stop();
}
}
}
These are the audio settings in the inspector of the object that has a box collider (is trigger) and these sound settings:
And this is the sound itself:
Upvotes: 0
Views: 59
Reputation: 90679
You are too far away!
You enabled SpatialBlend = 1
And in the Inspector you can see the falloff curve and the red line is where your audio listener is at => far too far away to really hear anything of the sound
You'll have to edit that roll-off in order to allow your audio listener to hear the sounds from further away.
In general: Is there a reason why adding that AudioSource
component on demand?
Rather already add it on your object right from the beginning, adjust the settings correctly and in your code only do
public override void PerformAction(GameObject obj)
{
audioSource = obj.GetComponent<AudioSource>();
if(audioSource) audioSource.Play();
}
public override void StopAction(GameObject gameObject)
{
if (audioSource)
{
audioSource.Stop();
}
}
Upvotes: 2