# Question How to implement Ammo Counter and Reload Function

Discussion in 'Getting Started' started by deruderu_gamedev, Jul 6, 2021.

1. ### deruderu_gamedev

Joined:
Jul 1, 2021
Posts:
17
Hello Unity community. Fisrt of all, please let me apologize if I made some sort of stupidity while asking for a question cuz I am not a native English speaker and very new to the game programming.

I started learning C# with unity by making nuclear throne types of clone game. By followin couples of youtube tutorial videos, I was able to achieve player movement, bullet shooting, and camera moving towards mouse cursor. Therefore, now, I want to implement ammo counter and reload function. I looked at other posts and tutorials about them, however couldnt make it work, so I am here to ask for the help.

Bullet Spawner:
Code (CSharp):
1. public class BulletSpawner : MonoBehaviour
2. {
3.     public GameObject bulletPrefab;
4.     public Transform shotPoint; // Bullet Spawner Position
5.
6.
7.     public float fireRate;     // Bullet Fire Rate
8.     float timeUntilNextShot;
9.
10.
11.     void Update() // Bullet Spawner Function
12.     {
13.         if (Input.GetMouseButtonDown(0) && timeUntilNextShot < Time.time) // Fire Input Function
14.         {
15.             Shoot();
16.             timeUntilNextShot = Time.time + fireRate; // Fire Rate Function
17.         }
18.     }
19.
20.     void Shoot() // Bullet Spawner Rotation Calculation
21.     {
22.         Vector3 mousePos = Input.mousePosition;
23.         mousePos.z = 5.23f;
24.
25.         Vector3 currentPos = Camera.main.WorldToScreenPoint(shotPoint.position);
26.         mousePos.x = mousePos.x - currentPos.x;
27.         mousePos.y = mousePos.y - currentPos.y;
28.
29.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
30.         Quaternion rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
31.
32.         Instantiate(bulletPrefab, shotPoint.position, rotation);
33.     }
34. }
35.
Bullet:
Code (CSharp):
1. public class Bullet : MonoBehaviour
2. {
3.     public float speed = 15f; // Bullet Speed
4.     public float lifeTime; // Bullet Life Time
5.     public GameObject destroyEffect; // Bullet Destroyed Effect
6.
7.
8.     Rigidbody2D rb;
9.
10.     private void Start()
11.     {
12.         rb = GetComponent<Rigidbody2D>();
13.     }
14.
15.     private void FixedUpdate() // Bullet Movement Calculation
16.     {
17.         Vector2 movement = transform.right * speed;
18.         rb.velocity = movement;
19.     }
20.
21.     void Awake() // Bullet Destroy Function
22.     {
24.     }
25. }
AmmoCounter:
Code (CSharp):
1. public class AmmoCounter : MonoBehaviour
2. {
3.     [SerializeField]
4.     public int _ammoCount = 15;
5.
6.     public int ammo;
7.     public int maxAmmo = 15;
9.
10.     void Start()
11.     {
12.         ammo = maxAmmo();
13.     }
14.
15.     void Shoot()
16.     {
17.         AmmoCount(-1);
18.
19.         if (Input.GetMouseButtonDown(0) && timeUntilNextShot < Time.time)
20.         {
21.             if (_ammoCount == 0)
22.             {
24.             }
25.             if (_ammoCount == 0 && canReload)
26.             {
29.             }
30.             if (Input.GetKeyDown(KeyCode.R))
31.             {
32.                 ammo = maxAmmo;
33.             }
34.         }
35.     }
36.
37.     void AmmoCount(int bullets)
38.     {
39.         _ammoCount += bullets;
40.     }
41. }