I have been making an fps and I am adding ammunition but I am testing it and the ammo goes down at more than one at a time so it goes from 10 to 6 when you click once to fire.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
public int maxammo = 10;
private int currentammo;
public float reloadtime = 1f;
public float damage = 100f;
public float range = 10000000f;
public Camera fpscam;
// Use this for initialization
void Start () {
currentammo = maxammo;
}
// Update is called once per frame
private void Update()
{
if (currentammo <= 0)
{
Reload();
return;
}
if (Input.GetButton("Fire1"))
{
Fire();
}
}
void Reload() {
Debug.Log("reloading");
currentammo = maxammo;
}
void Fire()
{
currentammo--;
RaycastHit Hit;
if (Physics.Raycast(fpscam.transform.position,fpscam.transform.forward, out Hit, range))
{
Debug.Log("hit");
Target target = Hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}