- Home /
How do I add sound to this script
I have a script for shooting my gun (you can have if you wish) I need to make it so i can add a audio to this script like a gun shot sound
Script \/
var prefabBullet:Transform;
var shootForce:float;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
Answer by aldonaletto · Jun 13, 2011 at 03:22 AM
Add an audioSource to your gun, drag the sound you want to shootSound in the Inspector and fire the sound using:
var shootSound:AudioClip;
function Update(){
if (Input.GetButtonDown("Fire1")){
audio.PlayOneShot(shootSound);
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
Thank you very much it worked good. A few small errors just had to add a { in a spot no biggie. Thank you very much this helps alot!
Where would the { go? please contact me by email edwardclark48@yahoo.com
I am getting errors with this script. Can you fix this?
Answer by ShockdeadGames · Mar 17, 2015 at 09:02 PM
I edited the script a little just for the sound. I did this:
var shootSound:AudioClip;
function Update(){ if (Input.GetButtonDown("Fire1")){ } audio.PlayOneShot(shootSound)};
Now I get a bunch of errors. What did I do wrong?
Your answer
Follow this Question
Related Questions
Random Gun Jam with Array 1 Answer
Reload after certain amount of shots? 1 Answer
How can I locally play audio over a network without rapid RPC calls? 2 Answers
Particle deletes itself after unchecking one shot 0 Answers
mecanim gun shot frame 1 Answer