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 TheUltimatePVP · May 14, 2016 at 08:38 PM · animationjavascriptsound

Need help with automatic weapon firing!

Hello! I have a problem. My automatic weapon fires way too fast and whenever it shoots the sound doesnt finish. It's like "ba-ba-ba-ba-ba-ba-ba-bang (bang is when I let go of left mosue button) I don't know how to fix this so any help would be appreciated. Here is my code: function Update () { if (Input.GetButton("Fire1")) { var gunsound : AudioSource = GetComponent. (); gunsound.Play(); GetComponent. ().Play("MachineGin_shoot"); GlobalAmmo.CurrentAmmo -= 1; } }

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

2 Replies

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

Answer by SMJMoloney · May 14, 2016 at 10:53 PM

There's two ways to go about this. If you simply want to let the audio play you can say this

 function PlayGunSound()
 {
     if (gunsound.isPlaying) return;
     gunsound.Play();
 }

This will ensure that the full sound plays.

If you want to slow down the gun and be sure the full sound plays, you'll need a timer. Declare a two floats at the top like this

 public var speed;
 var timer;

In the Update function, say this

 timer += Time.deltaTime;

Finally, when you fire the gun say this

 if (Input.GetButton("Fire1") && timer >= speed)
 {
     PlayGunSound();
     timer = 0;
 }

In the editor, you can say how fast the gun is by setting the speed value.

UPDATE:

You have to use a line like this

 var gunsound : AudioSource = GetComponent(AudioSource);

You can declare that at the top and it should get your audio source.

In the end, it could look something like this

 #pragma strict
 
 public var speed;
 var timer;

 var gunsound : AudioSource;
 var anim : Animation;

 function Start()
 {
     gunsound = GetComponent(AudioSource);
     anim = GetComponent(Animation);
 }

 function PlayGunSound()
 {
     if (gunsound.isPlaying) return;
     gunsound.Play();
     anim.Play("MachineGin_shoot"); //Should that be MachineGun?
     GlobalAmmo.CurrentAmmo--;
     timer = 0;
 }

 function Update()
 {
     timer += Time.deltaTime;
 
     if(Input.GetButton("Fire1") && timer >= speed)
         PlayGunSound();
 }
Comment
Add comment · Show 14 · 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 TheUltimatePVP · May 17, 2016 at 12:17 PM 0
Share

Whenever I do what you say S$$anonymous$$J, I get "Object reference not set to an instance of an object" and The animation and sound wont work.

avatar image SMJMoloney TheUltimatePVP · May 17, 2016 at 12:52 PM 0
Share

I've updated the original post. I don't know if that GlobalAmmo.CurrentAmmo statement works. I'm not sure of your setup but is GlobalAmmo another script attatched to the same gameObject?

avatar image TheUltimatePVP SMJMoloney · May 17, 2016 at 01:22 PM 0
Share

No Global Ammo isn't attached to the same GameObject

Show more comments
avatar image SMJMoloney TheUltimatePVP · May 17, 2016 at 01:36 PM 0
Share

Don't worry about being new. Gotta start somewhere.

Simply saying GlobalAmmo.CurrentAmmo won't work. I recommend putting an ammo variable in this script also like this

 public var speed, maxAmmo;
 var timer, currentAmmo;

 function Start()
 {
     currentAmmo = maxAmmo
 }

 function PlayGunSound()
 {
     if (gunsound.isPlaying) return;
     gunsound.Play();
     anim.Play("$$anonymous$$achineGin_shoot"); //Should that be $$anonymous$$achineGun?
     currentAmmo--;
     timer = 0;
 }

If you must use the other script, get it like the AudioSource and Animation

 var globalAmmo : GlobalAmmo;
 
 function Start()
 {
     globalAmmo = GameObject.Find("NameOfObjectScriptIsOn").GetComponent(GlobalAmmo);
 }

 function PlayGunSound()
 {
     if (gunsound.isPlaying) return;
     gunsound.Play();
     anim.Play("$$anonymous$$achineGin_shoot"); //Should that be $$anonymous$$achineGun?
     globalAmmo.currentAmmo--;
     timer = 0;
 }

Note: for globalAmmo.currentAmmo--; to work, currentAmmo has to be public in the globalAmmo script. You're using Javascript so I'm not entirely sure it's necessary to say public. I use C# mostly so I'm not as familiar.

avatar image TheUltimatePVP SMJMoloney · May 17, 2016 at 01:46 PM 0
Share

Okay the variable in my GlobalAmmo script is public. I have no compiler errors and my gun is working animation and sound, but when I go to the inspector on my game object and change my speed of the gun, the speed wont change. It waits for the sound to be completely over before I can shoot again. I want the sounds to overlap each other, like a real gun would.

Show more comments
avatar image TheUltimatePVP · May 17, 2016 at 12:19 PM 0
Share

I also get, "GetComponentFastPath can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene."

avatar image TheUltimatePVP · May 17, 2016 at 12:26 PM 0
Share

Code right now (Non-working) public var speed; var timer;

function Update () { timer += Time.deltaTime; if(Input.GetButton("Fire1") && timer >= speed) { var gunsound : AudioSource = GetComponent.(); gunsound.Play(); GetComponent.().Play("$$anonymous$$achineGin_shoot"); GlobalAmmo.CurrentAmmo -= 1; } }

avatar image
0

Answer by Unity_scat · May 14, 2016 at 10:53 PM

Set up a delay system. Something like:

 var lastShot : float = 0;
 var delay : float = 0.2f;
 
 function Update() {
 lastShot -= Time.deltaTime;
 if(Input.GetButton("Fire1") && delay <= lastShot) {
 //firing code
 lastShot = delay;
 }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

bullet destroy on collison 1 Answer

Journey like terrain modification 0 Answers

I want my character to enter and exit car with animation 1 Answer

Player attacking enemy script 0 Answers

Network animations playing on all player models whenever any player moves. 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