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 AlienNova · Jul 14, 2016 at 03:36 AM · c#mathbasic programming

Am I missing something when it comes to basic math?

Okay, I have this really small reloading function, that is called when I press 'R'

 int clipAmmo;//Is set to clipSize in Awake ()
 int clipSize = 7;
 int totalAmmo = 14;
 
 void Reload () {
         if (totalAmmo >= clipSize) {
             clipAmmo = clipSize;
             totalAmmo -= clipSize;
         } else {
             clipAmmo = totalAmmo;
             totalAmmo = 0;
         }//else
     }//Void Reload

Looks good right? Somehow when ever I press 'R' to reload, clipAmmo and tottalAmmo sets itself to 0. I have zero clue in what's going on because this is just simple math. WHAT AM I DOING WRONG?

Any help is appreciated.

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

1 Reply

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

Answer by Arkaid · Jul 14, 2016 at 05:44 AM

The code and the math is fine. So I'm guessing that Reload is getting called repeatedly when you press R, quickly depleting your ammo to 0

Also, this is prettier and easier to read:

  void Reload () 
  {
     // load the clip with whichever is the smallest amount
     clipAmmo = Mathf.Min(clipSize, totalAmmo);
     
     // reduce total ammo by amount filled
     totalAmmo -= clipAmmo;
 }
 

That said, that code doesn't check if the clip is already full (which is also why you're depleting ammo on reload). This will fix that:

 void Reload () 
 {
     // how many bullets can we fill?
     int fill = clipSize - clipAmmo;
 
     // load the clip with whichever is the smallest amount
     clipAmmo += Mathf.Min(fill , totalAmmo);
     
     // reduce total ammo by amount filled
     totalAmmo -= fill;
 }


Comment
Add comment · Show 2 · 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 AlienNova · Jul 14, 2016 at 12:19 PM 1
Share

It was around 11P$$anonymous$$ when I ran into this issue and forgot all about the difference between Get$$anonymous$$eyDown and Get$$anonymous$$ey. So you were rightl.

avatar image Bunny83 · Jul 14, 2016 at 01:45 PM 0
Share

Note: The second solution provides different mechanics than the original code. It actually "refills" the current clip ins$$anonymous$$d of changing the clip. It of course depends on what functionality you want. In reality if you change a clip which isn't yet empty those bullets are "wasted" / stay in that removed clip. A lot shooters that claim to be "realistic" do it that way.

Of course there are a lot of games that don't really have "clips" but an amout of loaded bullets and some reserve ammo (like Half-Life, Unreal, ...). So firing a single bullet and then reload would simply replace that one bullet in the clip ins$$anonymous$$d of replacing the clip as a whole.

The different mechanics have a huge impact on tactical reloading strategies of the players.

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

178 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Implement the equation as a code? 1 Answer

How would you fake air resistance? (c#) 2 Answers

Player rapidly accelerating instead of stopping 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