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
0
Question by VicciGames · Jul 23, 2012 at 01:23 AM · fpsshootingrayreload

Rayshoot Reload Problem (Can still shoot)

With my rayshoot script, when I manually reload (press R), I can constantly keep pressing r and reloading. By this I mean:

  • I have 25 bullets out of 32 bullets. I can press R to reload, now what happens is I can keep pressing R and then the sound will play everytime I press it. I can also keep shooting. Pressing R does not extend the reload time.

I want this to stop, but the problem is is that I do not know how. Please help, thank you in advance.

THE FULL SCRIPT IS NOT POSTED HERE

Here are some parts to the script:


Reload Sound

 void PlayReloadAudio(){
  
  
  audio.PlayOneShot( ReloadSound);
  
  
  }



What I use to indicate a reload, and the use of reloading

 BulletsLeft--;
      
      if( BulletsLeft < 0){
      
      BulletsLeft = 0;
      
      }
      
      if(BulletsLeft == 0){
      
      Reload(); 
      
      }
      }
 
 //and this
                     if(BulletsLeft < BulletsPreClip && Input.GetKeyDown(KeyCode.R)) {
       Reload();
  }



Reloading Part

 void Reload (){
  
  StartCoroutine( ReloadSpeed());
  
  }
  
  IEnumerator ReloadSpeed(){
  
  
  PlayReloadAudio();
  
  muzzleFlash.emit = false;
  Light1.active = false;
  Light2.active = false;
  Light3.active = false;
  
     yield return new WaitForSeconds(ReloadTime);
  
  
  if(Clip > 0){
  
  BulletsLeft = BulletsPreClip;
  
  }
  }
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 AlucardJay · Jul 23, 2012 at 04:08 PM 0
Share

add a boolean for example var canShoot : boolean = true; , and set it to false while reloading, then true when loaded. Then use this boolean when you fire e.g.

 if (Input.Get$$anonymous$$ouseButton(0) && canShoot) {..shoot!;}

2 Replies

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

Answer by SheepHugger · Jul 25, 2012 at 12:33 PM

Yes.

If the function for firing is something like:

 function Fire()
 {
     if (!isReloading)
     {
         //Do stuff, fire the gun
     } 
 }

Alternatively, the function call can be like this:

 if (Input.GetButton(&quot;Fire1&quot;) &amp;&amp; !isReloading)
 {
     Fire();
 }

In the latter, every time you press Fire1 button, it checks if the gun is not reloading. '!' in front is the same as

 if(isReloading == false)
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 VicciGames · Jul 26, 2012 at 11:00 PM 0
Share

i tried the boolean and it worked thanks! it was so simple...on behalf of my part im sorry i couldnt see something so simple! thanks again

avatar image
1

Answer by SheepHugger · Jul 23, 2012 at 07:13 PM

Or alternatively, the boolean can be something like:

 isReloading : boolean = false;

Also, set the timer for reloading:

 reloadTimer : float = 0;

And set how long it takes to reload:

 // It takes now 3.5 seconds to reload
 reloadTime : float = 3.5;

You can also have a variable for total amount of bullets that the player has, so he'll eventually run out:

 BulletsTotalLeft : int = 3 * BulletsPreClip;

then, if you run out of bullets in your clip or hit R and you still have bullets to reload into the gun but aren't currently reloading:

 // If your clip runs out of bullets
 if (BulletsLeft <= 0 && BulletsTotalLeft > 0 && !isReloading){
 Reload();
 }

 // If you hit 'R' to reload
 if (Input.GetKeyDown(KeyCode.R) && BulletsTotalLeft > 0 && !isReloading){
 Reload();
 }

And, the Reload() function will do something like function Reload(){ isReloading = true; reloadTimer = reloadTime; PlayReloadAudio(); }

Then, place this on Update(), to check if you're reloading and to countdown the time:

 function Update(){
 if (isReloading){
     reloadTimer -= Time.deltaTime;
     if (reloadTimer <= 0){
         // Reloading ends now
         isReloading = false;
         // Check if BulletsTotalLeft is less than BulletsPerClip
         if (BulletsTotalLeft < BulletsPreClip){
         // if there's less bullets total left than per clip, reload only that many
         BulletsLeft = BulletsTotalLeft;
         }
         else{
         // Otherwise just fill the clip
         BulletsLeft = BulletsPreClip
         }
     }
 }

Hope that helps you with what you're doing.

SheepHugger out.

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 VicciGames · Jul 24, 2012 at 07:36 PM 0
Share

I like the fast response, and the very descriptive path that you took to develop your answer. I already have all of what you stated (bullets per clip, clips, etc.) I'm just focusing on disabling the Shoot function while reloading. The boolean way may work, I haven't tried it yet. But is there a way to disable a function?

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

Gun reload and clip size 0 Answers

FPS Shoooting Problem 1 Answer

When I kill one enemy, they all die? 0 Answers

Reload FPS Animation Not Playing (Javascript) 1 Answer

Gun Shooting Animation Won't Play 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