How can i make if i have in magazine 30 ammos and in storage for example 100.
So if i shoot one ammo so its 29/100 and when i press R to reload and set 30/99
Here’s my gun script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpscam;
public ParticleSystem muzzleFlash;
public GameObject impacteffect;
private float nextTimetoFire = 0f;
private bool isReloading = false;
//AMMO
public int MaxAmmo = 10;
public int currAmmo;
public float reloadTime = 1f;
void Start()
{
currAmmo = MaxAmmo;
}
// Update is called once per frame
void Update()
{
if(isReloading)
return;
if(currAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if(Input.GetButton("Fire1") && Time.time >= nextTimetoFire)
{
nextTimetoFire = Time.time + 1f/ fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
currAmmo--;
RaycastHit hit;
if (Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hit, range))
{
if(hit.transform.tag == "Enemy")
{
CharacterStats enemyStats = hit.transform.GetComponent<CharacterStats> ();
if(enemyStats == null)
{
Debug.Log("You didn't hit an enemy");
} else
{
enemyStats.TakeDamage(damage);
}
if(hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGo = Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGo, 2f);
}
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(reloadTime);
currAmmo = MaxAmmo;
isReloading = false;
}
}