Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by joker71 · Apr 12, 2016 at 02:15 PM · gun scriptfire rate

A problem with GUN FIRE SOUND

I'm having a problem with an FPS game that I'm currently developing. When firing the gun, the sound of it is not very good and sort of "raw". When firing single shots it sounds just fine but when firing on full auto it gets distorted and doesn't sound like a gun supposed to. Also, it seems like while firing on full auto it skips some shot sounds.

Here's A sample that I've recorded of what I mean: http://picosong.com/NxUd/

This is the script I use:

 var ReloadSound : AudioSource; //reload audio source
 var DrySound : AudioSource; //empty magazine audio source
 var FireModeSound : AudioSource; //fire mode switching audio source
 var Shell : GameObject; // bullet shelll 3d object
 var ShellSpawn : Transform; // where the bullet shelll is going to be instantiated from
 public var ShootAnim : Animation; // the shooting animation
 public var ReloadAnim : Animation; // the reload animation (magazine is not empty)
 var ReloadEmptyAnim :Animation; // the reload animation (magazine is empty)
 public var IdleAnim : Animation; // the idle animation
 public var canShoot : boolean = true; // can the player shoot?
 public var canReload : boolean = false; // can the player reload?
 public var canAim : boolean = true; // can the player aim down sights?
 
 var clip = 30; // number of bullets in the magazine
 var ammoLeft = 320; // maximum ammo you can carry 
 var TimeBetweenShots : float = 0.09; // controlls the fire rate
 var timestamp : float;
 
 var Effect : Transform; // the effect that will be displayed at the bullet's hit position (like: bullet holes, blood, etc..)
 var TheDammage = 46; // the damage of a single bullet hit
 var AimSound : AudioSource; //aim audio source 
 
 var maxBulletSpreadAngle : float = 1.5f; // the bullet spread angle
 public var IsAiming : boolean = false; // is the player aiming?
 
 var MagazineDisplay : UnityEngine.UI.Text; // mag HUD
 var AmmoLeftDisplay : UnityEngine.UI.Text; // Ammo left HUD
 
 //Muzzle Flash Variables
 var MuzzleFlash : Renderer; // muzzle flash texture
 var MuzzleLight : Light; // point light
 
 var SmokePuff : GameObject; // smoke particle
 var SmokeSpawn : Transform; // position of the smoke particle
 
 public var FireSound : AudioClip; // audio file of a single shot
 var SoundSpawn: AudioSource; // audio source for the FireSound file
 
 var FullAuto : boolean = true; // boolean checking for full auto or semi auto
 
 var IsReloading : boolean = false; // is the player reloading?
 var finishReload : boolean = false; // player finished reloading?
 
 
 function Start () {
 
 MuzzleFlash.enabled = false; // disable the muzzle flash texture on start
 MuzzleLight.enabled = false; // disable the point light on start
 
 SoundSpawn = GetComponent.<AudioSource>();
 }
 
 function Update () {
    
    var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*2, Screen.height*2, 0));
    
    var fireDirection : Vector3 = transform.forward;
    
    var fireRotation : Quaternion = Quaternion.LookRotation(fireDirection);
    
    var randomRotation : Quaternion = Random.rotation;
    
    fireRotation = Quaternion.RotateTowards(fireRotation,randomRotation,Random.Range(0.0f,maxBulletSpreadAngle));
    
    
    if(Input.GetButton("Fire1") && clip > 0 && Time.time >= timestamp && canShoot == true && FullAuto == true){
        SoundSpawn.PlayOneShot(FireSound, 0.7F);
        clip -= 1;
        Fire();
 if (Physics.Raycast (transform.position,fireRotation*Vector3.forward, hit, 100))
         {
             Debug.DrawLine (transform.position, hit.point, Color.green);
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
     }
    
    if(Input.GetButtonDown("Reload") && clip != 0 && ammoLeft != 0 && canReload == true && IsReloading == false ){
        canAim = false;
        canShoot = false;
        Reload();
  }     
    
 
    if(clip == 0 && ammoLeft > 0 && IsReloading == false){
       ReloadSound.Play();
       ReloadEmptyAnim.Play("reload_empty");
       canAim = false;
       canShoot = false;
       AutoReload();
 
 
    }
       
    if(clip != 30 && ammoLeft != 0){
     canReload = true;
     
    }
       
 
    if(Input.GetButton("Fire1") && clip == 0 && ammoLeft == 0 && !ReloadAnim.IsPlaying("reload")){
       DryGun();
     }
 
 
 if(Input.GetButtonDown("FireMode") && FullAuto == true){
    
    FullAuto = false;
    FireModeSound.Play();
    
       }else if(Input.GetButtonDown("FireMode") && FullAuto == false){
    FullAuto = true;
    FireModeSound.Play();   
       }
       
       
       if(Input.GetButtonDown("Fire1") && clip > 0 && Time.time >= timestamp && canShoot == true && FullAuto == false){
        SoundSpawn.PlayOneShot(FireSound, 0.7F);
        clip -= 1;
        Fire();
 if (Physics.Raycast (transform.position,fireRotation*Vector3.forward, hit, 100))
         {
             Debug.DrawLine (transform.position, hit.point, Color.green);
             particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
     }
 
 
 MagazineDisplay.text = "" + clip;{
 AmmoLeftDisplay.text = " /" + ammoLeft;
 }
 
 
  
  
 
  }
 
                                                                                                                                                                                                         
 function Fire() {
         
  ShootAnim.Play("shoot");
 timestamp = Time.time + TimeBetweenShots;
          if(ShootAnim.IsPlaying("shoot"))
         ShootAnim.Rewind("shoot");
         
 }
 
 function Reload() {
 
 IsReloading = true;
 
 canAim = false;
 
 canReload = true;
 
 ReloadAnim.Play("reload_full");
 
 ReloadSound.Play();
    
 yield WaitForSeconds(3.7);
 
 var totalAmmo = clip + ammoLeft;
       if(totalAmmo <= 30){
           clip = totalAmmo;
           ammoLeft = 0;
       }else{
           var shotsFired = 30 - clip;
           clip = 30;
           ammoLeft -= shotsFired;
       }
 
 canShoot = true;             
 
 canReload = false;
 
 canAim = true;
 
 IsReloading = false;
 
 finishReload = true;
 
 yield WaitForSeconds(0.1);
 
 finishReload = false;
 }
 
 
 function AutoReload() {
 
 IsReloading = true;
 
 yield WaitForSeconds(3.7);
 
 totalAmmo = clip + ammoLeft;
       if(totalAmmo <= 30){
           clip = totalAmmo;
           ammoLeft = 0;
 
        }else{
            shotsFired = 30 - clip;
           clip = 30;
           ammoLeft -= shotsFired;
 
       }
 
 canShoot = true;   
 
 canAim = true;
 
 canReload = false;
 
 IsReloading = false;
 
 finishReload = true;
 
 yield WaitForSeconds(0.1);
 
 finishReload = false;
 
 }
 
 function DryGun(){
 
 DrySound.Play();
 
 }

Any Suggestions?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joker71 · Apr 14, 2016 at 04:15 PM 0
Share

Does anybody know why it sounds like that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Happy-Zomby · Apr 14, 2016 at 04:37 PM

Hi, I would recommend recording a full auto sound out of unity and playing one or the other depending on fire mode. It will help peformance and make scripting much much easier. I'm not sure but I'm guessing this is the standard practice. I found Audacity is a great freeware for pasting and merging sounds. Just set your sound to match the fire rate - e.g. 1 shot every 0.2 seconds and then remove ammo every 0.2 seconds (invoke repeating or coroutine). You can always stop the sound if user release the fire button or if out of ammo. Hope that helps,

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joker71 · Apr 15, 2016 at 06:38 AM 0
Share

Thanks. To be honest I'm pretty much a beginner in scripting, I got the general idea of what you suggested but I can't quite figure how to write the script. If you could write it down of maybe just some of the script it would be very helpful.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make my gun have a fire rate 0 Answers

Constant fire at target 2 Answers

how to make bullet go straight to middle of the screen 3 Answers

i need help with a gun recoil script 0 Answers

HELP 2D Top Down Shooter Bullets Only go Horizontal 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges