x


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);
       }
    }
}
more ▼

asked Mar 29 '12 at 01:52 AM

TeleportSanwich gravatar image

TeleportSanwich
1 1 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Mar 29 '12 at 02:10 PM

gregzo gravatar image

gregzo
1.6k 31 41 51

Thanks bro!

Apr 01 '12 at 01:30 AM TeleportSanwich

And it says Unknown Identifier "youCoolSoundEffect"...

Oct 03 '12 at 03:58 AM Arpian

yourCoolSoundEffect is the variable name used for the AudioClip. Read : http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html

Oct 03 '12 at 04:28 AM alucardj

alucardj, always so patient... I'm surprised you're not takng the time to explain what a variable and a type is.

Oct 03 '12 at 05:48 AM gregzo

Extraterrestrials (non-human) thank you for detailed answer.

Oct 03 '12 at 08:36 AM Arpian
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x527
x447
x68

asked: Mar 29 '12 at 01:52 AM

Seen: 480 times

Last Updated: Oct 03 '12 at 08:36 AM