x


how do you create sound when two objects hit one another?

I need to activate a sound clip when my first person controller drops an object with a box collider and rigid body onto another object with a box collider. any suggestions as to how i would achieve this?

more ▼

asked Nov 10 '10 at 07:26 AM

Joseph 1 gravatar image

Joseph 1
1 2 2 4

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

2 answers: sort voted first

There are these two things you want:

The condition being: A collision between 2 box colliders The reaction being : Play a sound file

These are actually 2 questions, do you have trouble with playing sound or detecting collisions or both?

Here's some suggestions that assume the worst case scenario:

Triggers

An alternative way of using Colliders is to mark them as a Trigger, just check the IsTrigger property checkbox in the Inspector. Triggers are effectively ignored by the physics engine, and have a unique set of three trigger messages that are sent out when a collision with a Trigger occurs. Triggers are useful for triggering other events in your game, like cutscenes, automatic door opening, displaying tutorial messages, etc. Use your imagination!

Your collision response could come from a trigger by objects carrying tag properties

Another idea may be to have the rigidbody of each object check for collisions (which the physics engine does ) catch the message using the suggestion spree just handed you and when you caugth the collision message you can use the same reaction you would in situation 1.

To play an audio file you will need to attach it to the object probably, and since collisions between different objects cause different sounds both objects should play the sounds maybe, up to you I guess what you want to hear.

To play sound, load the sound into a public variable with the type AudioSource

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour 
{
    public AudioSource myCampFireSoundThingy;

    public void someEvent() 
    {

        myCampFireSoundThingy.Play();
    }
}
more ▼

answered Nov 10 '10 at 03:14 PM

Proclyon gravatar image

Proclyon
1.4k 10 13 32

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

use OnCollisionEnter. You could attach an audiosource with the correct audioclip to your object and then OnCollisionEnter do audio.Play();

more ▼

answered Nov 10 '10 at 08:47 AM

StephanK gravatar image

StephanK
6k 39 53 93

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

x522
x193
x80
x71
x54

asked: Nov 10 '10 at 07:26 AM

Seen: 1727 times

Last Updated: Nov 10 '10 at 07:26 AM