- Home /
Question is off-topic or not relevant. simple math question and not unity related
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));
}
}
}
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;
}
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.
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
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