I’m creating a clone of a missile and it uses audio for the launch and explosion. I don’t think I quite have the hang of referencing other scripts, but here’s my best shot. I have one script for the clone and one for the original missile G.O., that gets destroyed once I instantiate the clone. The scripts build but the audio won’t work in the game.
here’s the relevant code from the MissileClone script:
var detonator : Detonator = GetComponent(Detonator);
var newBehaviourScript :NewBehaviourScript;
var missileScript:MissileScript = GetComponent(MissileScript);
var audiO : AudioSource = missileScript.GetComponent(AudioSource);
function Start(){
audiO.clip = missileScript.audioClip1;
audiO.Play();
}
function OnCollisionEnter(collision : Collision)
{
detonator.Explode ();
rigidbody.isKinematic=true;
Destroy(GetComponent(MeshRenderer));
audiO.clip = missileScript.audioClip2;
audiO.Play();
Destroy(missileScript.target);
WaitForSeconds(6);
newBehaviourScript.cameraBack ();
}
And here’s the relevant code from the MissileScript script: (this is all that’s relevant)
var audio :AudioSource;
var audioClip1 :AudioClip;
var audioClip2 :AudioClip;
I m a little confused with the missile and the clone. But let’s give it a try.
Create a missile object with everything you need: mesh, texture, script. Then create a prefab and drag and drop the full model.
The script on the missile may have a movement command. That is if it does not get a force.
It has an audio source. that plays on awake with the sound of the fouiiiiiich.
Then it has a collision detection like you have on your question with an explosion sound as such:
That will create an audio source object that destroys automatically on completion of the audio file independently of you missile already destroyed. On top of that the sound will come form the impact point, so if the collision is far away the sound is lower.
Now your guy might have something like:
var missile:GameObject;
function Update(){
if(Input.GetKeyDown(KeyCode.Space))Instantiate(missile, spawnPointPosition, rotation);
}
}
You drag and drop the prefab in the missile slot and that should work:
I would recommend to have a similar process for the explosion. Create a particle system for the explosion, Unity has one ready, and instantiate the particle on collision.
As I do not know your level of knowledge I might be telling you things you already know…