x


Play Random Sound On FPC Collision With Wall

Hi guys, I'm looking for a script to put on the First Person Controller that plays a random sound from a selection of audio clips when the FPC collides with a surface tagged 'Wall'.

I managed to assemble an OnControllerColliderHit script that did this but the sound played continuously until the player moved away from the wall. Anybody got a script that would play the sound just once until the player moved away then collided with the wall again?

Any suggestions will be greatly appreciated. Many thanks, Will

more ▼

asked Jun 12 '10 at 02:42 PM

Will 1 gravatar image

Will 1
77 9 9 10

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

2 answers: sort voted first

You should use OnCollisionEnter instead of OnCollisionStay.

If that don't work you need to set a flag to prevent multiple activations e.g.

if(canPlay && hitWall){
   audio.Play();
   canPlay = false;
}

After an amount of time without touching the wall you can set can play to true again.

As for random sounds you can put all the sounds into an array and then call a random array item each time you hit a wall e.g.

var sounds : GameObject[];

function PlayRandomSound(){
  Instantiate(sounds[Random.Range(0,sounds.length)],transform.position,transform.rotation);
}

Depending on how you managed sounds

more ▼

answered Jun 12 '10 at 02:58 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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

I have done something similar to this using OnCollisionEnter I believe. I had an array of AudioClips and I randomly picked on of the AudioClips and assigned it to a single AudioSource I had attached to my player. This way I could have all my settings set for my AudioSource (no looping, a set volume, 2D sound vs 3D sound, ect.) and I would just switch out the AudioClip and play the sound afterward.

more ▼

answered Jan 06 '11 at 05:38 PM

Kristen gravatar image

Kristen
16 1 1 6

(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:

x5070
x2488
x29

asked: Jun 12 '10 at 02:42 PM

Seen: 2043 times

Last Updated: Jun 12 '10 at 02:42 PM