x


Sound on collision

Hello all.

I have a simple script that makes a locker make 2 different sounds depending on what it collides with. If it collides with concrete it makes one sound and if it collides with another locker it makes a different sound.

Here it is:

var FloorSound : AudioClip;
var LockerSound : AudioClip;

function OnCollisionEnter (other : Collision) {
    if (other.gameObject.tag == "Concrete") {
        audio.clip = FloorSound;
        audio.Play();
    }
    else if (other.gameObject.tag == "Locker") {
        audio.clip = LockerSound;
        audio.Play();
    }
}

The problem I am getting is that at the start of the game the locker collides with the floor instantly and the sound goes off. Then because the locker never actually leaves the floor it doesn't make the sound again when I push it over. It collides with the other locker quite nicely so I know there isn't a problem as such with the code, it just needs something adding to it to keep it updating every time the locker collides with the floor.

more ▼

asked Apr 14 '10 at 10:24 AM

Sam Beckham gravatar image

Sam Beckham
55 3 3 14

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

3 answers: sort voted first

You could probably fix this by examining the relativeVelocity reported by the collision in the OnCollisionStay method, and playing a sound if it exceeds some threshold. You could even change the volume of the sound based on the relativeVelocity.

Something like this:
(for illustrative purposes! - untested, may contain errors!)

var FloorSound : AudioClip;
var LockerSound : AudioClip;
var impactSoundThreshold = 0.5;  // tweak this value

// both the "Enter" and the "Stay" collisions pass
// their data to the sound function:

function OnCollisionStay (collision : Collision) {
    PlayCollisionSound(collision);
}

function OnCollisionEnter (collision : Collision) {
    PlayCollisionSound(collision);
}

function PlayCollisionSound (collision : Collision) {

    // check if the collision was hard enough to generate a sound
    if (collision.relativeVelocity > impactSoundThreshold) {

        // select the correct sound effect based on the object's tag
        switch (collision.gameObject.tag) {
            case "Concrete": audio.clip = FloorSound; break;
            case "Locker": audio.clip = LockerSound; break;
        }

        // calculate the volume (anything above 4*Threshold = full volume)
        var volume = Mathf.InverseLerp(impactSoundThreshold, impactSoundThreshold*4, collision.relativeVelocity);

        // play the audio
        audio.volume = volume;
        audio.Play();
    }
}
more ▼

answered Apr 14 '10 at 02:00 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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

Figured it out now. It's very similar to what Duck sugested.

var LockerSound : AudioClip;

function OnCollisionStay (collision : Collision) {
        if (collision.relativeVelocity.magnitude > 0.5) {
            audio.clip = LockerSound;
            audio.Play();
            audio.volume = (collision.relativeVelocity.magnitude / 5 + 0.5);
            audio.pitch = (Random.value * 0.5 + 0.5);
            }
        }

I decided to take out the difference in sound files as this was complicating things a little and made some funny sounds but you can add it back in quite easily.

more ▼

answered Apr 14 '10 at 02:54 PM

Sam Beckham gravatar image

Sam Beckham
55 3 3 14

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

I am not a coder but I had something similiar ... If your locker is always on the concrete then you shouldn't use the collision with the concrete to activate the sound. Try to link it when you character is pulling it iPushLocker = true;. you should also consider looping the sound while you push...

var iPushLocker : boolean = false;


function OnCollisionEnter (other : Collision) {



 if((other.gameObject.tag == "Locker") && (iPushLocker == true ))
    {
        audio.clip = FloorSound;
        audio.Play();


    }


}

and then add onCollisionExit to set the IPushLocker as false. Hope it helps a bit.

more ▼

answered Apr 14 '10 at 11:02 AM

alexnode gravatar image

alexnode
984 27 32 49

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

x3460
x2499
x1703
x1029
x29

asked: Apr 14 '10 at 10:24 AM

Seen: 6736 times

Last Updated: Apr 14 '10 at 10:24 AM