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 /
This question was closed Dec 31, 2018 at 04:05 PM by hexagonius for the following reason:

Question is off-topic or not relevant. simple math question and not unity related

avatar image
0
Question by el-mas-pro-470 · Dec 31, 2018 at 01:40 PM · gunweaponammogun script

Total Bullets to weapon reload? Help

Hello! I have created a script that works perfectly, but there is something that is weird, The variable TotalAmmo is the number of bullets the player is towards weapon, and when reloading the spent bullets are subtracted until total ammo is 0. But when Total Ammo has 15 bullets for example and not enough to reach the charger, How would you make those bullets are the recharge? (In short if I have fewer bullets than the weapon that occupies recarge the few bullets that are left, that is total bullets). Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class Weapon : MonoBehaviour {
     
     public float Damage = 0f;
     public float Range = 0f;
     public float ImpactForce = 0f;
     public float FireRate = 0f;
     
     public int MaxAmmo = 0;
     public int TotalAmmo = 0;
     public int CurrentAmmo;
     public int DesechedBullets;
     public int ReloadBullets;
     public float ReloadTime = 0;
     public float DesechedBullTime = 0;
     private bool IsReloading = false;
 
     public Camera FPScam;
     public ParticleEmitter MuzzleFlash;
     public GameObject HitEffect;
     
     private float NextTimeToFire = 0f;
 
     public AudioClip ShootSound;
     
     void Start()
     {
         CurrentAmmo = MaxAmmo;
     }
     
     void OnEnable()
     {
         IsReloading = false;
     }
     
     void Update () {
         if(TotalAmmo <= MaxAmmo)
 
         if(DesechedBullets >= MaxAmmo)
         {
             DesechedBullets = MaxAmmo;
         }
 
         if(TotalAmmo <= 0)
         {
             TotalAmmo = 0;
             CurrentAmmo = 0;
             DesechedBullets = 0;
         }
         if(IsReloading)
         return;
 
         if(CurrentAmmo < MaxAmmo)
         {
         if(Input.GetKeyDown(KeyCode.R))
         {    
             StartCoroutine(Reload());
             return;
         }
         }
 
         if(Input.GetButton("Fire1") && Time.time >= NextTimeToFire)
         {
             NextTimeToFire = Time.time + 1f/FireRate;
             if(CurrentAmmo > 0)
             Shoot();
         }
     }
     
     IEnumerator Reload()
     {
         IsReloading = true;
         Debug.Log("Reloading...");
         
         yield return new WaitForSeconds(ReloadTime - 0.25f);
 
         
         CurrentAmmo = ReloadBullets;
         TotalAmmo -= DesechedBullets;
         DesechedBullets = 0;
         IsReloading = false;
 
     }
 
     void Shoot()
     {
         DesechedBullets++;
 
         MuzzleFlash.Emit();
         audio.Play();
         audio.clip = ShootSound;
         
         CurrentAmmo--;
         
         RaycastHit hit;
         if (Physics.Raycast(FPScam.transform.position, FPScam.transform.forward, out hit, Range))
         {
             Debug.Log(hit.transform.name);
             
             Target target = hit.transform.GetComponent<Target>();
             if(target != null)
             {        
                 target.TakeDamage(Damage);
             }
             
             if(hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * ImpactForce);
             }
             
             Instantiate(HitEffect, hit.point, Quaternion.LookRotation(hit.normal));
         }    
     } 
 } 
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

  • Sort: 
avatar image
0
Best Answer

Answer by betaFlux · Dec 31, 2018 at 02:09 PM

I don't know what the desechedBullets variable means, but I personally found the following code to work quite fine:

     public int MaxAmmo = 100;
     public int TotalAmmo = 100;
     public int ClipSize = 15;
     public int CurrentAmmo = 15; // full clip on start
     bool reloading;

     void Shoot() {

         CurrentAmmo--;
         if(CurrentAmmo <= 0)
             StartCoroutine(CR_Reload());
     }
     IEnumerator CR_Reload() {
 
         if(reloading == false) {
 
             if(TotalAmmo == 0) { // Ammo depleted, no reason to go on

                 reloading = false;
                 yield break;
             }
             else if(TotalAmmo < ClipSize) { // Don't reload a complete clip if there are only a few bullets left

                 CurrentAmmo = TotalAmmo;
             }
             else // Else simply assign the clip size
                 CurrentAmmo = ClipSize;
             reloading = true;
         }
         // Do sound and animation logic here
         yield return new WaitForSeconds(animationLength); // Wait until animation is done
            TotalAmmo -= Mathf.Clamp(ClipSize - CurrentAmmo, 0, MaxAmmo);
            reloading = false;
     }

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 el-mas-pro-470 · Dec 31, 2018 at 02:20 PM 0
Share

The DesechedBullets are the spent bullets that are subtracted when reloading! For example, if I shot once, DesechedBullets is 1 and so on repetitively. Pressing the R to "TotalAmmo" subtracts the DesechedBullets.

avatar image betaFlux el-mas-pro-470 · Dec 31, 2018 at 03:06 PM 0
Share

I think if you add a clip size limit, you can remove the DesechedBullets calculations. I edited the script and adapted the variable names to make it less confusing to understand.

What it does is: subtract one bullet every shot until TotalAmmo is depleted, this unrealistically keeps bullets that are left in the clip, like in the modern Fallout games.

Follow this Question

Answers Answers and Comments

100 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

Related Questions

Gun script not working 3 Answers

Gun with limited ammo? 1 Answer

Any problem with using real weapons in a FPS game? 0 Answers

ammo display javascript 1 Answer

how to access a specific instance of a script 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