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 ?
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.
– Lockstep