- Home /
How to add sound to gun shot script
So I got this code, that I'm using to make my gun shot in Unity.
var projectile : Rigidbody; var speed = 20; function Update() { if( Input.GetButtonDown( "Fire1" ) ) {
var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider ); } }
Could some one tell me how to update my code so, that everytime left mouse button "Fire1" is pressed, then the sound would be played. I'm not familiar with java scripts.
Sajamon Tuzkin Ps. Thnx!
Answer by Wopet · Sep 29, 2012 at 08:32 AM
var projectile : Rigidbody; var speed = 20; function Update() { if( Input.GetButtonDown( "Fire1" ) ) {
audio.Play(); var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider ); } }
Added that "audo.Play();" and shooting sound works nicely now.
Answer by DaveA · Sep 28, 2012 at 07:44 PM
I put your question into the Search box above and got this:
http://search.unity3d.com/uss1/?q=How+to+add+sound+to+gun+shot+script&type_of_search=answers
Answer by Jessespike · Sep 28, 2012 at 09:15 PM
public AudioClip teleportSound; // Add a AudioClip reference
audio.PlayOneShot(teleportSound); // Play the AudioClip
http://answers.unity3d.com/questions/278058/how-to-add-a-sound-to-this-script.html
Answer by jpizano · Jan 09, 2016 at 12:11 PM
if (Input.GetButtonDown("Fire1") { GetComponent().Play() }
Make sure to add an audio source to your gameObject.
This script usually works for me :)
Your answer
Follow this Question
Related Questions
How do I destroy a gameobject after 5 seconds 2 Answers
Advanced FPS shooting 1 Answer
Shooting script problem. 2 Answers
How can I shoot bullets that you can aim with the crosshair? 1 Answer
Using raycast gun to take down life of enemy when shot 1 Answer