Search Unity

Making a gun sound when shooting (HELP)

Discussion in 'Scripting' started by iWoundPwn, Dec 27, 2013.

  1. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Hello, I created this script so when you click it shoots the game object, I wanted it also to play a OneShotSound through a AudioClip public variable this is C# btw. But when I do this put the clip in the inspector it says I want an audiosource which I don't want because I want it to play through the script. Here is the script thanks!
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FireBlaster : MonoBehaviour
    5. {
    6.     public GameObject blaster;
    7.     private Transform myTransform;
    8.     private Transform cameraHeadTransform;
    9.     private Vector3 launchPosition = new Vector3();
    10.     private float fireRate = 0.2f;
    11.     private float nextFire = 0;
    12.     public AudioClip gunSound;
    13.    
    14.     void Start ()
    15.     {
    16.         myTransform = transform;
    17.  
    18.         cameraHeadTransform = myTransform.FindChild ("CameraHead");
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         if(Input.GetButton("Fire Weapon")  Time.time > nextFire)
    24.         {
    25.             nextFire = Time.time + fireRate;
    26.             launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);
    27.  
    28.             Instantiate(blaster,launchPosition,Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
    29.                                                                 myTransform.eulerAngles.y, 0));
    30.             audio.PlayOneShot(gunSound);
    31.         }
    32.     }
    33. }
     
  2. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Nevermind I fixed it thanks!