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 Therian13 · Jan 08, 2014 at 06:47 AM · gunammoreloading

having trouble with ammo reload script. destorys extra ammo

Hello Everyone, I have a basic script that allows me to reload a gun when it hits 0.

The only problem is that when I reload the gun while it has some rounds left inside, the current amount in the gun gets destroyed and a brand new 6 bullets are put it.
How can I make it that it only reloads up to 6 ammo without destroying the ones already in the gun?

I know there are other things in the script I need to fix as well, but this is the one error I cant seem to figure out. (the rest I'm sure I can figure out)

Any help would be greatly appreciated. Thank you!

here is the script

 var bulletType: Rigidbody;
 var bulletSpeed = 10;
 private var handgunROF = 0.5;
 
 var gunIsHeld : boolean = false;
 
 var playerAmmoCount:ammoCount;
 private var handgunFire :boolean = true;
 
 
  function Update ()
  {
  FireHandgun();
  ReloadHandgun();
  }
  
  
  
  function FireHandgun()
  {
  if (!gunIsHeld) 
     {
     return;
     }
     
     if (playerAmmoCount.roundsInRevolver > 0)
         {
          if (Input.GetButtonDown("Fire1")&& (handgunFire))
  
             {
             handgunFire=false;
              clone = Instantiate(bulletType, transform.position, transform.rotation);
              clone.velocity = transform.TransformDirection(Vector3(0,0, bulletSpeed));
      
              Destroy (clone.gameObject, 0.2);
              audio.Play();
              particleSystem.Play();
              playerAmmoCount.roundsInRevolver -=1; //subtracts 1 round from clip
              yield WaitForSeconds(handgunROF);
              handgunFire=true;
      
              }
          }
      }
      function ReloadHandgun()
      {
          if (Input.GetKeyDown("r"))
              {
                  yield WaitForSeconds(playerAmmoCount.reloadtime);
                  playerAmmoCount.roundsInRevolver +=6;
                  
                  if (playerAmmoCount.roundsRevolverAmmo <6)
                      {
                      playerAmmoCount.roundsInRevolver = playerAmmoCount.roundsRevolverAmmo;
                      playerAmmoCount.roundsRevolverAmmo -= playerAmmoCount.roundsRevolverAmmo;
                      }
 
                      else
                      {
                      playerAmmoCount.roundsRevolverAmmo -=6;
                      }
                      
              }
  }
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
0
Best Answer

Answer by GameVortex · Jan 08, 2014 at 08:23 AM

I can't see anything that destroys the bullets that are there, it adds 6 bullets to the the gun when you reload no matter how many bullets you have left in the gun. You need to add in only as many bullets that there is space for. So it is a bad practice to start with adding 6 bullets.

So we need to do a couple of things to get this right:

1: Calculate how many bullets we want to reload based on how many bullets are left in the gun.

2: Clamp that number down to the number of bullets we have left as spare ammo if it is less than the amount we want to reload.

3: Reload the ammo

Bonus 4: Also, no need to run the reload if we have no bullets left or have 6 bullets in the gun.

Example:

 if(playerAmmoCount.roundsInRevolver < 6 && playerAmmoCount.roundsRevolverAmmo > 0) //Bonus Step 4
 {
     yield WaitForSeconds(playerAmmoCount.reloadtime);
 
     float reloadAmount = 6 - playerAmmoCount.roundsInRevolver; //Step 1.
     
     reloadAmount = Mathf.Clamp(1, playerAmmoCount.roundsRevolverAmmo); //Step 2.
 
     //Step 3.
     playerAmmoCount.roundsRevolverAmmo -= reloadAmount;
     playerAmmoCount.roundsInRevolver += reloadAmount;
 }

Because it is a coroutine, you would probably want to add a bool so you can't start loading while loading.

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 Therian13 · Jan 10, 2014 at 04:13 AM 0
Share

thank you for your help! I'm sorry for not replying earlier. I usually receive an email to let me know someone replied, but it hasn't done it. But I fixed it. It is very close to yours but thank you for re$$anonymous$$ding me to fix bool so you cannot load while loading. I didn't even think about that!

Thank you again!

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

19 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

Related Questions

Multiple Cars not working 1 Answer

gui help for ammo display 1 Answer

I Need Help Whit Gun! 2 Answers

C# Reload Script 1 Answer

Gun - Ammo , reloading and UI problem. 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