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();
}
}
}
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
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!
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?
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()
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.
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;
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)
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: