I have 8 empty game objects around the camera and I want the same sounds to come from those points in the world.
How can I PlayClipAtPoint correctly to accomplish this.
This script is on my Audio Manager Empty game object. But i want the audio to come from Point1-Point10 transforms position.
public class AudioManagerScript : MonoBehaviour
{
public Transform point1;
public Transform point2;
public Transform point3;
public Transform point4;
public Transform point5;
public Transform point6;
public Transform point7;
public Transform point8;
// Use this for initialization
public AudioClip clip1;
public AudioClip clip2;
public AudioClip clip3;
public AudioClip clip4;
public AudioClip clip5;
public AudioClip clip6;
public AudioClip clip7;
public AudioClip clip8;
public AudioClip clip9;
public AudioClip clip10;
public AudioSource audio;
void Start()
{
InvokeRepeating("myFunc", 1.0f, 1.0f);
audio = GetComponent<AudioSource>();
}
void myFunc()
{
audio.PlayClipAtPoint(clip1,point1.transform.position,0.9f);
}
}
Two things:
- Use arrays
- Don’t use PlayClipAtPoint
I’ll take the second suggestion first. My spider-sense suggests to me that you’re using PlayClipAtPoint simply because that’s the audio playback function you’re most comfortable with - would that be accurate? Or do you have a more specific reason for using PlayClipAtPoint? You could get much more straightforward results by attaching an AudioSource to each of these transforms - you could set the AudioClip of each of those in the AudioSource itself, rather than a bunch of AudioClip variables here.
As for an array (this assume you’ve taken the advice above):
public AudioSource[] sources;
void myFunc() {
sources[0].Play();
}
The array will be resizable, and you could use a for loop to do something with all of them - though I’d need a little more info on how this is being used to make a good recommendation as to what that for loop would look like.
1 Like
As for PlayClipAtPoint itself: That’s a static function, not a function of an instance. So you wouldn’t declare your own AudioSource in order to use it, but just use the class name directly:
AudioSource.PlayClipAtPoint(clip1,point1.transform.position,0.9f);
1 Like
I have 100 clips in an Audio folder named 1.mp3,2.mp3,3.mp3… I want to play they all randomly at each of the 8 empty games objects in my scene every second. Its to mimic what a schizophrenic hears. The Empty game objects are point 1-8.
I’m guessing
for(1-100)
{
sources = load audio from project not sure exact sintax
}
Ok, so I can see some wisdom in using PlayClipAtPoint for that - that way each clip will play until it finishes, and if you play a clip at the same spot, it won’t interrupt the previous clip (which is something you’d have to work around if using AudioSource’s).
Alright, so back to AudioClips and Transforms. You do want an array of both. You have a bunch of clips in a folder, so you’ll want to put that folder inside a Resources folder, and use Resources.LoadAll to fill your array:
public AudioClip[] clips;
public Transform[] points;
void Start() {
clips = Resources.LoadAll<AudioClip>("yourfoldername");
}
This way you don’t need to drag in audio clips one by one into your inspector.
The Transform array, you can expand in the inspector and drag Transforms into there (I assume just as you’ve been doing). Having it as an array will make your code simpler and more versatile than as a bunch of individual variables.
I gather you just want to pick one of your point and play a clip at that point each time myFunc is called. In that case, a for loop is actually not necessary:
void myFunc() {
AudioClip chosenClip = clips[Random.Range(0, clips.Length) ];
Transform chosenPoint = points[Random.Range(0, points.Length) ];
AudioSource.PlayClipAtPoint(chosenClip, chosenPoint.position, 0.9f);
}
1 Like
Sweet you did it. Thanks a ton!