Struggling with ammo ui and making gun reload in C# unity

Hey! So I’m in school for a digital gaming and multi media degree, right now for my assignment I have to code UI for a gun.
I have it so my gun starts off with 20 bullets, the ammo counter counts down with it, and it when I run out of ammo, the gun stops shooting. Great! but!
the UI counting the bullets aways goes ‘20, 0, 19, 18, 17, etc.’. I have no clue why it spits out the zero second, and then returns to normal.
also my ‘press r’ to reload doesn’t work at all. I would love some help on this. thanks!

here’s the code thus far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class playerShot : MonoBehaviour
{
    // Assign a Rigidbody component in the inspector to instantiate

    public Rigidbody Bullet;
    public float maxAmmo = 20f;
    public float currentAmmo;
    public Text ammoCount;
    private float reloadAmount;

    private void Start()
    {

    }
    void Update()
    {
        reloadAmount = maxAmmo - currentAmmo;
        if (Input.GetButtonDown("r"))
        {
            currentAmmo = reloadAmount + currentAmmo;
            ammoCount.text = currentAmmo.ToString();
        }

        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Jump"))
        {
            currentAmmo = maxAmmo -= 1;

            if (currentAmmo > 0)
            {
                Rigidbody clone;
                clone = Instantiate(Bullet, transform.position, transform.rotation);
                clone.velocity = transform.TransformDirection(Vector3.forward * 10);
            }
        
        }
    }
}

sorry if it is a mess. I’m much more a modeler/tech artist. Still getting the hang of coding.

//reloadAmount = maxAmmo - currentAmmo;<-----you dont need this if currentAmmo is going to be at max value after reload
            if (Input.GetButtonDown("r"))
            {
                //currentAmmo = reloadAmount + currentAmmo;
                //dont need to do math if it going to be max, math are taking performance
                currentAmmo = maxAmmo;
                ammoCount.text = currentAmmo.ToString();
            }
if (Input.GetButtonDown("Jump"))
            {
                //leave maxAmmo value alone
                currentAmmo--;

                if (currentAmmo > 0)
                {
                    Rigidbody clone;
                    clone = Instantiate(Bullet, transform.position, transform.rotation);
                    clone.velocity = transform.TransformDirection(Vector3.forward * 10);
                }

            }

now test it tell me how it goes

1 Like

edit sorry forgot one line , put this under currentAmmo–;
currentAmmo = Mathf.Clamp(currentAmmo, 0, maxAmmo);

1 Like

It is working perfectly with this code :smile: (Very much thanks to you!)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class playerShot : MonoBehaviour
{
    // Assign a Rigidbody component in the inspector to instantiate

    public Rigidbody Bullet;
    public float maxAmmo = 20f;
    public float currentAmmo;
    public Text ammoCount;



    private void Start()
    {
        currentAmmo = maxAmmo;
    }
    void Update()
    {

        if (Input.GetButtonDown("reload"))
        {
            currentAmmo = maxAmmo;
            ammoCount.text = currentAmmo.ToString();
        }

        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Jump"))
        {
            currentAmmo = Mathf.Clamp(currentAmmo, 0, maxAmmo);
            ammoCount.text = currentAmmo.ToString();

            if (currentAmmo > 0)
            {
                currentAmmo = currentAmmo -= 1;

                Rigidbody clone;
                clone = Instantiate(Bullet, transform.position, transform.rotation);
                clone.velocity = transform.TransformDirection(Vector3.forward * 10);
            }

        }
    }
}
2 Likes

i see you change currentAmmo–; to currentAmmo = currentAmmo -= 1; and put it inside if state
and that make currentAmmo = Mathf.Clamp(currentAmmo, 0, maxAmmo); useless the reason for currentAmmo = Mathf.Clamp(currentAmmo, 0, maxAmmo);
is there is to stop currentAmmo–; from going -1 or over maxAmmo. if currentAmmo -= 1 inside if state you dont need Mathf.Clamp(currentAmmo, 0, maxAmmo) any more its wasting progress.

1 Like

Changing it back, thanks for catching that!

no problem , and this my last tips:

//you cant shoot more then one bullet
currentAmmo--;
currentAmmo = Mathf.Camp(currentAmmo,0,maxAmmo);

//you can shoot more then one bullet

currentAmmo -= 2;
currentAmmo = Mathf.Camp(currentAmmo,0,maxAmmo);

or

currentAmmo = currentAmmo - 2;
currentAmmo = Mathf.Camp(currentAmmo,0,maxAmmo);

or

currentAmmo = currentAmmo - =2;
currentAmmo = Mathf.Camp(currentAmmo,0,maxAmmo);
        private void Update()
        {

            if (Input.GetButtonDown("r"))
            {
                ReloadAmmo();
            }

            if (Input.GetButtonDown("Jump"))
            {
                Shoot();
            }
        }

        private void ReloadAmmo()
        {
            currentAmmo = maxAmmo;
            ammoCount.text = currentAmmo.ToString();
        }

        private void Shoot()
        {
            currentAmmo = currentAmmo -= 1;
            currentAmmo = Mathf.Clamp(currentAmmo, 0, maxAmmo);
            ammoCount.text = currentAmmo.ToString();

            if (currentAmmo == 0)
            {
                //you can add auto reload here if you want
                //ReloadAmmo();

                return;//<-----This will skip all the code below no bullet prefab will be create
            }

            //with return; on currentAmmo == 0 all the code here will be skip if your currentAmmo zero
            Rigidbody clone;
            clone = Instantiate(Bullet, transform.position, transform.rotation);
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }