Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by marsfan · Jul 23, 2013 at 11:44 PM · waitreloadingsecondsguns

how to make gun wait until done reloading?

I have a code written for my gun, and as soon as it reloads it begins shooting again, how do I make it wait until the sound for reloading the gun is done playing (it is 3 seconds).

I have my code attached and I would like to know what I need to add to make it wait until the audio finishes reloading.

 #pragma strict
 var reloadSound: AudioClip;
 var fireSound: AudioClip;
 var bullet : GameObject;
 var bulletsPerSecond = 14.0;
 private var shooting = false;
 var bulletForce = 1000.0;
 var bulletsInMag = 30;
 var magsLeft = 10;
 private var reloading = false;
 var reloadCount = 0;
  
 function Start() {
    InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
    InvokeRepeating("Reload", 0.0, 1.0);
 }
  
 function Shoot() {
   if (!shooting) return;
      var go = Instantiate(bullet, transform.position, transform.rotation);
      go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
      audio.PlayOneShot(fireSound);
      bulletsInMag --;
 }
  
 function Update(){
     shooting = false;
     Debug.Log(bulletsInMag);
     if(Input.GetAxis("Fire1") && bulletsInMag > 0){
         shooting = true;
     }else if(Input.GetAxis("Fire1") && bulletsInMag <= 0){
     shooting = false;
     reloading = true;
     }
 }
 
 function Reload(){
     if (!reloading) return;
         audio.PlayOneShot(reloadSound);
         bulletsInMag = 30;
         reloading = false;
 }

I can cut out parts of the code if people need it cleaned up.

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
0
Best Answer

Answer by robertbu · Jul 24, 2013 at 02:14 AM

Why use InvokeRepeating for Reload()? Why not call it directly on line 33? Also, you can put a yield between lines 40 and 41 to pause the 3 seconds before reloading.

 yield WaitForSeconds (3.0);
Comment
Add comment · Show 6 · 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 marsfan · Jul 24, 2013 at 02:28 PM 0
Share

I tried that and It would just stop and not reload when I was out of bullets, rending the gun useless.

avatar image robertbu · Jul 24, 2013 at 04:01 PM 0
Share

Here is a bit of restructuring:

 #pragma strict
 var reloadSound: AudioClip;
 var fireSound: AudioClip;
 var bullet : GameObject;
 var bulletsPerSecond = 14.0;
 private var shooting = false;
 var bulletForce = 1000.0;
 var bulletsIn$$anonymous$$ag = 30;
 var magsLeft = 10;
 private var reloading = false;
 var reloadCount = 0;
  
 function Start() {
    InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
 }
  
 function Shoot() {
   if (!shooting) return;
     var go = Instantiate(bullet, transform.position, transform.rotation);
     go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
     audio.PlayOneShot(fireSound);
     bulletsIn$$anonymous$$ag--;
 }
  
 function Update(){
     shooting = false;
     Debug.Log(bulletsIn$$anonymous$$ag);
     if(Input.GetAxis("Fire1") && bulletsIn$$anonymous$$ag > 0 && !reloading){
         shooting = true;
     }else if(bulletsIn$$anonymous$$ag <= 0){
     shooting = false;
     Reload();
     }
 }
  
 function Reload(){
     reloading = true;
     audio.PlayOneShot(reloadSound);
     bulletsIn$$anonymous$$ag = 30;
     yield WaitForSeconds(3.0);
     reloading = false;
 }
avatar image marsfan · Jul 24, 2013 at 06:23 PM 0
Share

That did it, thanks, and why was waitforseconds stopping it when I had it the way I did above?

avatar image robertbu · Jul 24, 2013 at 06:33 PM 0
Share

Without seeing the code exactly as you had it, I cannot be sure. One problem is that your reloading was contingent on 'Input.GetAxis("Fire1")'. Another problem was that your Reload() would be called by your Invoke() multiple times during the time the Reload() was waiting resulting in multiple coroutines. Neither issue should have stopped the gun from working at all.

avatar image marsfan · Jul 26, 2013 at 02:19 PM 0
Share

It looked just like the above code you posted, but wait for seconds was before bullets in mag

Show more comments
avatar image
0

Answer by chris_taylor · Jul 24, 2013 at 12:59 AM

 if(Input.GetAxis("Fire1") && bulletsInMag > 0){

should also have !reloading

 if(Input.GetAxis("Fire1") && bulletsInMag > 0 && !reloading){

but from what I can see the reloading looks instant, if you are playing a animation somewhere you should test if the animation is done before setting reloading to false.

*Edit: I would also only check for the input and if you need to reload when the weapon can fire so in this case in the shoot methods you are invoking

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

gun wont reload 1 Answer

having trouble with ammo reload script. destorys extra ammo 1 Answer

Reloading wont work 1 Answer

Yield And Return 2 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