- Home /
AudioSource.PlayClipAtPoint
Hi, I have a problem that I hope someone can help me with, I want to play a sound where an RPG hits the ground, so what I have done is made an RPG from the basic game object cylinder, then I dragged it into the prefab folder to make a prefab from it.
I then initiate the prefab when I fire the RPG and it fires fine, I've added a audio source to the RPG with the setting as 3D, Custom RollOff with a distance of 30, also attached to the RPG prefab is a script that initiates an explosion when colliding with anything and I also thought it would play the explosion sound at the same point, this is the bit of code I use to initiate the explosion and sound -
function OnCollisionEnter(collision : Collision) {
if(!loop){
loop = true; // Set loop to true so this destroyRPG() function dont keep repeating
var contact : ContactPoint = collision.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
// Instantiate the explosion and fireball facing away from the impact point
Instantiate(explosion, transform.position + Vector3(0, -0.15, 0), rot);
Instantiate(rpgExplosion, transform.position + Vector3(0, 0, 0), rot);
AudioSource.PlayClipAtPoint(explosionSound, collision.contacts[0].point);
destroyRPG();
}
}
But no matter where the RPG lands the sound is constant, is this incorrect ?
Answer by Lockstep · May 03, 2013 at 04:08 PM
The audio source itself does not decide wether it plays a 3d sound or a 2d sound. You have to mark the clip itself as 3d or 3d sound. Select the audio clip in the project panel and check the corresponding check box in the inspector.
Furthermore AudioSource.PlayClipAtPoint creates its own AudioSource and destroys it automatically when the clip is finished. So you don't need to attach a AudioSource component to your RPG.
Thanks for the input, my sound are all set to 3D, but I didn't know AudioSource.PlayClipAtPoint creates its own AudioSource and destroys it automatically when the clip is finished, so that means I need the set it to 3D, Custom RollOff with a distance of 30 at runtime with something like below ?
private var RPGexplosion: AudioSource;
function Awake(){
RPGexplosion = AddAudio(fireSoundEnd, false, false, 0.5, AudioRolloff$$anonymous$$ode.Custom, 40);
}
function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float, RollOff$$anonymous$$ode: AudioRolloff$$anonymous$$ode, $$anonymous$$axDistance: float): AudioSource {
var newAudio = gameObject.AddComponent(AudioSource);
newAudio.clip = clip;
newAudio.loop = loop;
newAudio.playOnAwake = playAwake;
newAudio.volume = vol;
newAudio.rolloff$$anonymous$$ode = RollOff$$anonymous$$ode;
newAudio.maxDistance = $$anonymous$$axDistance;
return newAudio;
}
I don't know how exaclty your setup works, but I usually solved this by having the audio source directly on the explosion prefab. You can see every property in the inspecot and change it to your liking, then save the prefab to keep the changes. Since the explosion only needs to play one single clip, it is sufficient to set the clip to your explosion clip, check playonawake and uncheck loop. Then you only need to make sure the explosion lives long enough to play the whole clip.
Your answer
Follow this Question
Related Questions
A better script for a machine gun? 1 Answer
How to route sound from PlayClipAtPoint() to a specific audio mixer group? 2 Answers
Adjust properties of AudioSource created with PlayClipAtPoint 2 Answers
Making AudioSource follow camera? 1 Answer
Why am I able to play an AudioClip without an AudioSource? 1 Answer