- Home /
Question by
aidengaming123 · Apr 19, 2013 at 03:28 AM ·
fpssoundgunshoot
Question about Gun Sounds
I'm using a Rigid Body shooting script that I got from a tutorial, and it's working for what I'm making (A Multiplayer Scifi FPS), but I want to add a gun sound for when you shoot the gun.
Here;s the original script:
var projectile : Rigidbody; var speed = 150;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 5);
}}
And here's how I added the one part (keep in mind, I'm a giant noob when it comes to scripting)
var projectile : Rigidbody;
var speed = 10;
var sound : AudioClip;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 5);
}}
function Start(){
audio.clip = sound;
}
function Update (){
if(Input.GetButtonDown(0)){
audio.Play();
}
}
When I try to use it, I can't even shoot the gun :/ Please help!
Comment
Answer by ExpiredIndexCard · Apr 19, 2013 at 05:16 AM
you are using the Update() function twice.
You are using the same if statement - just worded differently.
Try using this:
var projectile : Rigidbody;
var speed = 10;
var sound : AudioClip;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
audio.Play();
Destroy (clone.gameObject, 5);
}
}
function Start(){
audio.clip = sound;
}
Your answer
Follow this Question
Related Questions
How to add sound to gun shot script 4 Answers
FPS PISTOL 1 Answer
Temporarily disable audio? 1 Answer