Question by
samueltosi · Apr 27, 2013 at 04:15 AM ·
audiosoundshootinggun
How to script shooting to trigger shooting sound
I have a script in which i shoot or "throw" boxes I can't seem to figure out how to insert a code to play a throwing sound under my if (Input.GetButtonDown("Fire2")) { section. If anyone knows the proper way to trigger the audio segment please let me know. Heres my whole gun script.
using UnityEngine;
using System.Collections;
public class gun : MonoBehaviour {
public GameObject bulletPrefab;
public float bulletImpulse = 100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire2")) {
Camera cam = Camera.main;
GameObject bullet = (GameObject)Instantiate(bulletPrefab, cam.transform.position, cam.transform.rotation);
bullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);
}
}
}
Comment
Answer by andreyazbyn · Sep 09, 2015 at 11:39 AM
ad this under the if(Input.GetButtonDown("Fire2")
AudioSource.PlayClipAtPoint(reloadAudio,new Vector3(firePoint.position.x, firePoint.position.y, firePoint.position.z));
and ad this variable
public AudioClip fireAudio;