Hello everyone! My name is Alan and I am following the course for some time. I was at the #87 lecture and I found a small issue with the AudioSource.PlayClipAtPoint. Why? Well, because I’m so stuborn that I used Unity 5 to make the game, instead following Ben’s advice.
For those who don’t know, in Unity 5 you can’t change the audio from 3D to 2D in the import settings tab. You need to have a Game Object created for that sound, and there you can change this property in a slider called “Spatial Blend”. By default, Unity 5 make the audio 3D and you can’t change this parameter for the PlayClipAtPoint method. So the result is an “unhearable” break sound event.
I’m not really sure if someone here have already proposed an workaround for this issue. And also I’m not really sure if my solution is a good solution, but for those who are so stuborn as I am, here is a script for make the job done.
public class SoundManager : MonoBehaviour {
// Configuration of properties
[Range(0.0f, 1.0f)]
public float volume = 1.0f;
[Range(0.0f, 1.0f)]
public float spatialBlend = 0f;
[Range(-3.0f, 3.0f)]
public float pitch = 1.0f;
[Range(-1.0f, 1.0f)]
public float pan = 0f;
// List of the created instances of One Shot Player
private List<GameObject> soundPlayerChilds;
// Use this for initialization
void Start () {
soundPlayerChilds = new List<GameObject>();
}
// Update is called once per frame
void Update () {
// Finds whose sounds are already done playing and kill'em'all
for (int i = soundPlayerChilds.Count-1; i >= 0; i--)
{
if (!soundPlayerChilds[i].GetComponent<AudioSource>().isPlaying)
{
Destroy(soundPlayerChilds[i]);
soundPlayerChilds.RemoveAt(i);
}
}
}
public void OneShotSound(AudioClip audioClip, Vector3 position)
{
// Add a new instance for the list of instances
GameObject dummyGameObject = new GameObject("One Shot Sound");
soundPlayerChilds.Add(dummyGameObject);
int currentIndex = soundPlayerChilds.Count - 1;
// Add the audio source component
soundPlayerChilds[currentIndex].AddComponent<AudioSource>();
soundPlayerChilds[currentIndex].transform.position = position;
// Get the audio source component created
AudioSource audioSource = soundPlayerChilds[currentIndex].GetComponent<AudioSource>();
// Configure the audio source component
audioSource.clip = audioClip;
audioSource.volume = volume;
audioSource.spatialBlend = spatialBlend;
audioSource.pitch = pitch;
audioSource.panStereo = pan;
// Starts playing the sound
audioSource.Play();
}
}
You need to attach this script (I called it “SoundManager”) to a Game Object. When you select it in the hierarchy you can change some basic properties for the sound to play in the inspector tab.
After that you just need to retrieve the reference to the Game Object with something like:
soundManager = FindObjectOfType<SoundManager>();
And play the punctual sound with the OneShotSound method:
soundManager.OneShotSound(breakSound, transform.position);
The class will create a temporary object to play the sound and also will take care of the destruction of the objects that already finished playing the sound.
Well, I hope my contribuition is useful for someone and I’m really sorry for my bad english!
Just one more thing: Happy New Year for you all! =D
See ya!