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 Yehsuah · Apr 11, 2016 at 07:39 AM · ammo

The correct math for calculating remaining Ammo in Mag?

OK, so I followed Sebastian's tutorials on a Top Down Shooter in unity, and I made a slight modification to how the ammo reloads. There are 3 variables: currentAmmoInMag, ammoPerMag, and totalAmmo.

I have the math correct(for what seems) both. I know for a fact I have the currentAmmoInMag reload correct.

In the tutorial, there is 10 bullets in 1 mag, and 40 total bullets(4 Mags). Well, if you were to only shoot 2 bullets and then reload, your total ammo would decrease by 10. Not right. Well anyways; heres the math, if you need more info(like the rest of the script), just let me know:

(This is performed on reload, BTW)

currentAmmoInMag += ammoPerMag - currentAmmoInMag;

totalAmmo -= ammoPerMag - currentAmmoInMag;

Heres a visualization:

Current/Total

10/40

You Shoot 2 bullets

8/40

When you reload

10/38

THATS how it should look, and right now, TotalAmmo doesn't change. It just stays at 40.

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 Yehsuah · Apr 11, 2016 at 07:01 AM 0
Share

I have solved my own problem, and incase anyone has the same issue; all I did was create a public int named shotsFired; and defined it as ammoPer$$anonymous$$ag - currentAmmoIn$$anonymous$$ag and the set the total ammo EQUAL(not subtract) to totalammo - shotsFired

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mrtt789 · Jul 16, 2021 at 06:01 AM

here is my Code which works perfect

 void Start()
 {
     currentAmmo = magazineCapacity;
     Debug.Log(totalAmmo);
 }
 public void reload()
 {
     //if totalAmmo itself have enough bullets
     if (totalAmmo>=magazineCapacity)
     {
         //get the ammo required to fill the magazine
         neededAmmo = magazineCapacity - currentAmmo;
         //decrement the ammo required to fill the magazine from total 
         totalAmmo -= neededAmmo;
         //then increment the ammo required to fill the magazine  
         currentAmmo += neededAmmo;
     }
     //if total ammo is less then the magazineCapacity
     else if(totalAmmo>0)
     {
         //get the ammo required to fill the magazine
         neededAmmo = magazineCapacity - currentAmmo;
         //if totalAmmo has enough bullets required to fill the magazine
         if(neededAmmo<totalAmmo)
         {
             totalAmmo -= neededAmmo;
             currentAmmo += neededAmmo;
         }
         //if totalAmmo is less then the required neededAmmo then incerement all the bullets in the currentAmmo from totalAmmo
         else 
         {
             currentAmmo += totalAmmo;
             totalAmmo = 0;
         }
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[CLOSED] {SOLVED} how could i add an ammo script to this? 1 Answer

how do i add ammo and reload to my script: this is what ive tried http://pastebin.com/wPybHpys 0 Answers

Bullet Counter Not Working Propperly 0 Answers

Player will not stop shooting when ammo count = 0 1 Answer

Limit ammo 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