SOUND on Collision

Hello Everybody,
I’m doing a car simulator and now I have a problem. Please Help me!!!
I’d want to reproduce the audio crash when my car is going to bang on the wall.
I have this script but it doesn’t work.

using UnityEngine;
using System.Collections;

public class CollisionSound : MonoBehaviour {
	public Transform objectgame;
	public AudioClip brakeSound;
	private AudioSource brakeAudioSource;
	
	
	// Use this for initialization
	void Start () {
		brakeAudioSource =gameObject.AddComponent<AudioSource>();
		brakeAudioSource.clip=brakeSound;
		brakeAudioSource.loop=false;
		brakeAudioSource.volume=1.0f;
		brakeAudioSource.Stop();
		
	}
	
	// Update is called once per frame
	void Update () {

	}
	void OnCollisionEnter(Collision Collision) {
if(Collision.gameObject ==objectgame) {
	audio.clip=brakeSound;
			audio.Play();

}	
}
}

Ciao,

first of all, are you sure “audio.Play();” is being called?

I think it’s right but i can’t hear anything…
I’ already tried also:
if(Collision.gameObject ==objectgame) {
brakeAudioSource.clip=brakeSound;
audio.clip=brakeSound;
audio.Play(); }

or:
if(Collision.gameObject ==objectgame) {
brakeAudioSource.Play(); }
…but it doesn’t work :frowning:

To elaborate on fedefevi’s point, you might want to place a Debug.Log statement within the if statement so you are sure the collision is actually being detected. The problem might be with your colliders, not the audio. Just another thing to check. Good luck!

Joe

Wouldn’t you be trying to find “Other” as opposed to “Collision.gameobject” ?

ok, it works :slight_smile: … thank you !!!

i tried to copy this part into my project

audio.clip=brakeSound;
audio.Play();

and replace the “brakesound” for a sound i have here… and it keeps saying that is a unknow identifier…

i have to attach the sound somewhere?

did you write this part in the script?

void Start () {
brakeSound =gameObject.AddComponent();
brakeSound.clip=brakeSound;
brakeSound.loop=false;
brakeSound.volume=1.0f;
brakeSound.Stop();

}

oh… no i didnt tough it was nescessary =D… thanks man!

so if i do this, i dont need to attach the soundfile to any object, i just need to have it imported on the project folder, and then call it with that script?

no no, you have to create a variable AudioClip:

public AudioClip brakeSound;
private AudioSource brakeAudioSource;

and then you can attach an audio file to the script.

Couple of points:

If you are creating a wheeled vehicle. You need to check the collider and make sure it is not a wheel that is colliding. (probably in the scripts section here, but not that hard to create)

If you want multiple sounds, you will want to create a sound Instance script. This will create a sound the tell it to destroy it’s self after the sound is finished and make sure that it is not a looping sound. Also, this method allows you to create sounds at an impact location. You will want to up your min distance on your sound to accommodate the camera audio listener. (I found the script for that here someplace.)

If creating instance sounds, you will want to constrain the volume to the velocity of the impact. (its like collision.velocity /10 or something like that)

if creating instance sounds and your player is creating the sounds, you will want to turn Doppler off in your sound or it will sound real bad. (look up doppler in the resource)

If you are creating instance sounds, you may wish to limit the number of sounds created by the same object to every .25 seconds. (simple as adding a variable that says nextSoundAt=Time.time + 0.25;)

You can vary the same sound by adjusting it’s pitch. (I used 0.5 +Random.value():wink:

You can accomplish different types of sounds depending on the “state” of the collision. (i.e. OnCollisionStay should be a scraping sound OnCollisionEnter should be a bang sound.) Beware, OnCollisionStay is the length since the last OnCollisionStay event. The velocity of this event can produce larger numbers.

well… i tried everthing but… it still doesnt work

lemee just see if i can undestand this…

there can only be ONE audio source in the object right?.. but the object… in this case is the main character, and i want him to make more than one sound

like… i need a sound for jump, a sound for landing, a sound when get hit… lots of’em…

can i attach one audio source and then change the audio file on that source by scripting?

In my car simulator, i attached more than one audio source on my car.
I write this simple scritp:

public AudioClip CollisionAudio;
public AudioClip Frenata;

private AudioSource CollisionAudioSource;
private AudioSource brakeAudioSource;

public void Start()
{
CollisionAudioSource =gameObject.AddComponent();
CollisionAudioSource.clip=CollisionAudio; //your clip
CollisionAudioSource.loop=false; // chiose if you want the sound repeat
CollisionAudioSource.volume=1.0f; //volume of the clip
CollisionAudioSource.Stop(); //stop the audio at the start. (play in update)

brakeAudioSource =gameObject.AddComponent();
brakeAudioSource.clip=BrakeAudio;
brakeAudioSource.loop=true;
brakeAudioSource.volume=1.0f;
brakeAudioSource.Stop();
}

and, in Update() you’ll write brakeAudioSource.Play(); or CollisionAudioSource.Play(); when you need them.
Ok ?

hey man you listen to me… i got it! iit works man!! its working!

you know you’re the man right? be aware of that you are the man, man

I’m a few months new to Unity3D and game programming (hi school class). If you have seen the Worminator Tutorial by TornadoTwins (UTube) then that’ll make it easier to explain. Haven’t seen it?: There are turrets a worm made out of 3 sphere game objects. Both shoot a sphere from a spawnPoint tagged as fireBall and/or wormProjectile. It has a Sphere Collider and a rigid body. I am trying to get an audio clip, a crackling fire sound, to play when the turrets get destroyed by my worm’s fireball.

Note: I realized I could just use Tornado’s Turret Collision script instead of making a new script afterward. OnFire script isn’t all connected. It’s pretty jumbled so some of what I wrote there is not part of any function or previously connected scripting b/c I know some of what I wrote/copied/pasted from you and others can play audio but I don’t know how to get it to.
Scripts:

Never Mind I figured it out

dang, complier error happened when i made the script

Please do not necro post. This thread is 13 years old!

Please read the Community Code of Conduct .