I have been making a simple FPS game and have a fairly good script but no matter what i search i can’t find a solution that works what i want is a system that auto reloads after the mag of 30 bullets is shot or reloaded with the R key at any point i have completed the reload with r key
Here is my code if anyone has a solution
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float velocity;
private int Ammo;
Animator anim;
private int One = 1;
void Start()
{
Ammo = 30;
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown (0))
{
if (Ammo > 0)
{
Fire ();
Ammo = Ammo - One;
}
else;
{
anim.SetTrigger ("Reload");
Ammo = 30;
}
}
if (Input.GetKeyDown (KeyCode.R))
anim.SetTrigger ("Reload");
Ammo = 30;
}
void Fire()
{
var bullet = (GameObject)Instantiate (
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * velocity;
Destroy(bullet, 5.0f);
}
}