- Home /
gun fire sound
hi guys, im trying to get a sound to play when the gun shot is fired, i think i am getting the hang of java script but i am getting errors and no sound
(MissingMethodException: Method not found: 'UnityEngine.AudioSource.play'.)
here is the code i am using to fire the cannons
var projectile : Rigidbody; var speed = 20; var gunSound : AudioSource;
function Update () {
if(Input.GetButtonDown("Fire1")) { clone = Instantiate(projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed)); gunSound.play();
Destroy (clone.gameObject, 2);
}
}
Answer by Estevominador · Jun 13, 2012 at 02:46 PM
and the right code is:
var sound : AudioClip; function... audio.Play(sound);
And you have to make sure to put an "Audiosource" in the gun.
see ya
Answer by Bunny83 · Apr 04, 2012 at 06:41 PM
Play is a function and like all functions they start with a capital letter.
haha i actually corrected that after i posted but to no avail, still having problems with it. as soon as i fire it 'crashs'
have even tried changing it to this, but does not seem to work either :S. granted it doesnt crash anymore, but still no sound
var projectile : Rigidbody; var speed = 20; var shootSound:AudioClip;
function Update () {
if(Input.GetButtonDown("Fire1")) { audio.PlayOneShot(shootSound); clone = Instantiate(projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed)); Destroy (clone.gameObject, 2);
}
}
Your answer
Follow this Question
Related Questions
Gun Firing help? 2 Answers
using ironsights wont work standing still 0 Answers
Make a Gun fire multiple times 3 Answers