- Home /
Automatic repeated sound
I'm making a Geometry Wars/Asteroids kind of top down shooter. I'm using raycasts as my bullets and I have a script that shoots the raycasts at an interval, simulating an automatic laser (see below.) Is there some way I could make a "laser blast" sound play at the same time that the raycast is shooting? I want to add sound to the laser, but don't know how to do it. Can someone please help?
P.S Here's the code I use for my laser
var fireRate : float = 0.5;
var Range : float = 1000;
var Force : float = 75;
private var nextFire : float = 0.0;
function Update(){
if(Input.GetButton("Fire1")&& Time.time > nextFire){
nextFire=Time.time + fireRate;
RayShoot();
}
}
function RayShoot () {
var Hit : RaycastHit;
var DirectionRay =transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * Range , Color.blue);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
if(Hit.rigidbody){
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
}
}
}
Answer by gregzo · Mar 29, 2012 at 02:10 PM
audio.PlayOneShot(yourCoolSoundEffect,volume);
Where yourCoolSoundEffect is an audioClip (simply drag and drop a wav file in your project window to import it as an AudioClip), and volume is a float between 0.0 and 1.0.
Read a bit on AudioClip and AudioSource in the docs to get a better grasp of what you're doing.
Serve fresh.
Enjoy!
And it says $$anonymous$$ Identifier "youCoolSoundEffect"...
yourCoolSoundEffect is the variable name used for the AudioClip. Read : http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html
alucardj, always so patient... I'm surprised you're not takng the time to explain what a variable and a type is.
Extraterrestrials (non-human) thank you for detailed answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065648im_/https://answers.unity.com/themes/thub/images/avi.jpg)