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 /
avatar image
0
Question by ToastandBananas · Jun 15, 2018 at 09:26 PM · unity 5uiunity 2dsound

Gunfire Sound Gets Cut Off When I Release the Aim Button. Help?

So for my game you have to hold down right mouse click to aim and then simultaneously click the left mouse click to shoot. When the gun is fired, I'd like the gunfire sound to play. Currently, I have this working, but I have a problem. When I let go of the aim button, the gunfire sound immediately stops playing (if it hasn't already played through).

I understand this is happening because of the placement of my PlayShootSound method call, but what I can't figure out is where to put it so that I don't have this problem.

Any pointers/ideas?

 public class Weapon : MonoBehaviour {
     
     public float fireRate = 0;
     public float damage = 10;
     public LayerMask whatToHit;
 
     public Transform BulletTrailPrefab;
     public Transform MuzzleFlashPrefab;
 
     float timeToFire = 0;
     float timeToSpawnEffect = 0;
     public float effectSpawnRate = 10;
     Transform firePoint;
 
     public AudioClip shootSound;
     private AudioSource gunFire;
 
     bool isShooting = false;
 
     // Use this for initialization
     void Awake () {
         gunFire = GetComponent<AudioSource>();
 
         firePoint = transform.Find("FirePoint");
         if (firePoint == null)
         {
             Debug.LogError("No fire point...Please make an empty object named FirePoint and attach to end of gun. :)");
         }
     }

 void Update () {
         CheckIfShooting();
     }
 
     void CheckIfShooting()
     {
         if (Input.GetButton("Fire2")) // Holding right click
         {
             if (fireRate == 0)
             {
                 print("fire rate is 0");
                 if (Input.GetButtonDown("Fire1")) // Left click while holding right click
                 {
                     Shoot();
                     PlayShootSound();
                 }
             }
             else
             {
                 if (Input.GetButton("Fire1") && Time.time > timeToFire) // For automatic guns
                 {
                     timeToFire = Time.time + 1 / fireRate;
                     Shoot();
                     PlayShootSound();
                 }
             }
         }
     }
 
     void Shoot()
     {
         Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100f, whatToHit);
         
 
         if (Time.time >= timeToSpawnEffect)
         {
             Effect();
             timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
         }
         // Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
 
         if (hit.collider != null)
         {
             // Debug.DrawLine(firePointPosition, hit.point, Color.red);
             Debug.Log("We hit " + hit.collider.name + " and did " + damage + " damage.");
         }
     }
 
     void PlayShootSound()
     {
         gunFire.PlayOneShot(shootSound, 1f);
     }
 
     void Effect()
     {
         Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); // Fire Bullet
 
         Transform muzzleFlashClone = Instantiate(MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
         muzzleFlashClone.parent = firePoint;
         float size = UnityEngine.Random.Range(6f, 9f); // Randomize size of muzzle flash
         muzzleFlashClone.localScale = new Vector3(size, size, size);
         Destroy(muzzleFlashClone.gameObject, 0.05f);
     }
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 ToastandBananas · Jun 18, 2018 at 09:27 PM 0
Share

I found the answer finally!!!!

I followed this tutorial on how to create an audio manager and call sounds through it...and now my problem is fixed. :)

https://www.youtube.com/watch?v=HhF$$anonymous$$tiRd0qI

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ToastandBananas · Jun 16, 2018 at 04:26 PM

I found the answer finally!!!!

I followed this tutorial on how to create an audio manager and call sounds through it...and now my problem is fixed. :)

https://www.youtube.com/watch?v=HhFKtiRd0qI

Comment
Add comment · 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
1

Answer by crawniik · Jun 15, 2018 at 11:56 PM

It may not be the best fix, but you COULD make an IEnumerator method for yield return new WaitForSeconds(waitTime); where waitTime is the same amount of time that the gunshot sound is, and then add a line under your PlayShootSound(); for WaitTime(); or whatever you want to name the wait method.

I'm a newb, so there may be better options out there that I just don't know about yet.

Comment
Add comment · 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

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

220 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 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 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 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

Image as boundary 1 Answer

Screen scaling causing text to be outside button 0 Answers

How to shrink text size 1 Answer

How do i prevent the player from dragging a ui element off screen? 1 Answer

All UI buttons appear and stack on top of each other. 1 Answer


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