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 adrennalinpro · Apr 22, 2014 at 04:24 AM · reloadammo

How to add the needed amount to the AmmoCount (current ammo)?

So I'm creating an ammo script for an AK47. The clip holds 30 bullets, and the player can hold up to 4 clips (120 bullets) at one time. I would like to make the script deduct the necessary integers from the TotalAmmo variable. I know that it is something like AmmoCount = AmmoCount + something;, but I can't seem to figure out what the "something" is. I guess I need a refresher in math :P. Basically, I want to ask Unity to find X, and X is the number that adds up to the clip. Find X, and we have our script.

Please excuse my messy script, as I am a tad new. I've been coding for about 2 years now, here and there, and I need much practice.

BTW this is also my fire script, and part of my animation script (I use the CanFire variable to determine if the animation can play or not. So if you can't fire, like if ammo is 0, then it won't play the firing animation.)

Anyone willing to help? Greatly appreciated... --Emsiardy

 var speed : float = 60;
 var SoundLocation : GameObject;
 var pickup : AudioClip;
 var projectile : GameObject;
 var fireRate : float = 0.09;
 var reloadRate : float = 0.01;
 var AmmoGUI : GUIText;
 var AmmoCount : float = 30;
 var Clip : float = 30;
 var TotalAmmo : float = 120;
 var CanReload : boolean = false;
 var CanFire : boolean = true;
 private var nextFire : float = 0.0;
 private var nextReload : float = 0.0;
 
 var MathProblemHere; // Just to remind myself where to fix the reload int
 
 function Update () {
 
 
         if(Input.GetButton ("Fire1") && Time.time > nextFire && CanFire == true) {
             nextFire = Time.time + fireRate;
             AmmoCount --;
             var clone : GameObject = 
             Instantiate(projectile, transform.position, transform.rotation) as GameObject;
             AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position);
             clone.rigidbody.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
             Destroy(clone, 9);
         }
         if(Input.GetButtonDown("Reload") && CanReload == true && AmmoCount < TotalAmmo && TotalAmmo > 0 && Time.time > nextReload) {
         nextReload = Time.time + reloadRate;
         AmmoCount = AmmoCount + MathProblemHere;
         //deduct from TotalAmmo and add to AmmoCount the necessary integer here
         }
         if(AmmoCount <= 0) {
         AmmoCount = 0;
         CanFire = false;
         }
         if(AmmoCount > 0) {
         CanFire = true;
         }
         if(AmmoCount >= Clip) {
         AmmoCount = Clip;
         CanReload = false;
         }
         if(AmmoCount < Clip) {
         CanReload = true;
         }
     }
     
 function OnGUI() {
 
     AmmoGUI.text = "Ammo: " + AmmoCount + " | " +  TotalAmmo.ToString(); 
     AmmoGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
     
 }
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 Adamcbrz · Apr 22, 2014 at 04:36 AM

Your code could be something similar to the following:

 var loadingClip = Mathf.Min(Clip, TotalAmmo)-AmmoCount;
 TotalAmmo -= loadingClip;
 AmmoCount += loadingClip;

I think that will do what your asking.

Comment
Add comment · Show 9 · 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 adrennalinpro · Apr 22, 2014 at 04:43 AM 0
Share

This code doesn't really do much, how would you suggest implementing it?

avatar image adrennalinpro · Apr 22, 2014 at 04:46 AM 0
Share

What about this?

 var speed : float = 60;
 var SoundLocation : GameObject;
 var pickup : AudioClip;
 var projectile : GameObject;
 var fireRate : float = 0.09;
 var reloadRate : float = 0.01;
 var AmmoGUI : GUIText;
 var AmmoCount : float = 30;
 var Clip : float = 30;
 var TotalAmmo : float = 120;
 var CanReload : boolean = false;
 var CanFire : boolean = true;
 var loadingClip = $$anonymous$$athf.$$anonymous$$in(Clip, TotalAmmo) - AmmoCount;
 private var nextFire : float = 0.0;
 private var nextReload : float = 0.0;
 
 
 function Update () {
 
 
         if(Input.GetButton ("Fire1") && Time.time > nextFire && CanFire == true) {
             nextFire = Time.time + fireRate;
             AmmoCount --;
             var clone : GameObject = 
             Instantiate(projectile, transform.position, transform.rotation) as GameObject;
             AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position);
             clone.rigidbody.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
             Destroy(clone, 9);
         }
         if(Input.Get$$anonymous$$eyDown("r") && CanReload == true && AmmoCount < TotalAmmo && TotalAmmo > 0 && Time.time > nextReload) {
         nextReload = Time.time + reloadRate;
         TotalAmmo -= loadingClip;
         AmmoCount += loadingClip;
 
         //deduct from TotalAmmo and add to AmmoCount the necessary integer here
         }
         if(AmmoCount <= 0) {
         AmmoCount = 0;
         CanFire = false;
         }
         if(AmmoCount > 0) {
         CanFire = true;
         }
         if(AmmoCount >= Clip) {
         AmmoCount = Clip;
         CanReload = false;
         }
         if(AmmoCount < Clip) {
         CanReload = true;
         }
     }
     
 function OnGUI() {
 
     AmmoGUI.text = "Ammo: " + AmmoCount + " | " +  TotalAmmo.ToString(); 
     AmmoGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
     
 }
avatar image Adamcbrz · Apr 22, 2014 at 04:47 AM 0
Share

From you question it sounds like all you are looking for is the math to find the number of bullets to load. This would go into your reload logic. Is there something I am missing in your question?

avatar image adrennalinpro · Apr 22, 2014 at 04:49 AM 0
Share

No you're right.. I guess I was just looking for a quick fix!!! Ha. Thanks though! This got me on the road to eventually get where I'm going.

avatar image adrennalinpro · Apr 22, 2014 at 04:51 AM 0
Share

But wait.. doesn't $$anonymous$$ath.$$anonymous$$ just find the smallest number??? Like, between 1 and 5, it would just return 1 wouldn't it???

Show more comments

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Shooting speed help! 1 Answer

Ammo with ammobar problem. 0 Answers

C# Reload Script 1 Answer

Buying Ammo HELP 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