Adding Ammo Pickups In Zombie Runner

How’d you go with adding ammo pickups?

Hi Rick,

I’m loving the course so far. Not too fast paced and also not too slow.

Quick question, though, regarding the part where you find the Ammo component from the Player. Is it faster (and safer) to simply do other.GetComponent<Ammo>().IncreaseCurrentAmmo(ammoType, ammoAmount) than using FindObjectOfType<Ammo>()? Is there any advantage in using FindObjectOfType?

Good question and one which I don’t think there is really a definitive answer. The rule that we always follow is “dont put a FindObjectOfType” in update if you can avoid it because there is some overhead to it. GetComponent is similar but in theory not quite as heavy. For the way that we are using it I think either of the 2 works fine.

2 Likes

All weapons ready! Still not sure if I want to use zombies or maybe something like Evil Robots of Doom.

Oh, I also found out that sniper rifle ammo is called NATO.

1 Like

Hey sir, I know this topic is old but I really was wondering how can I add ammo caps to this system?
For example:
Pistol Bullets can max out at 50
Shotgun Shells can max out at 125
AR Bullets can max out at 250 and the list goes on. I would be really glad if you could give me some hints.

Maybe in AmmoSlot just make a new public variable maxAmmoAmount and then clamp the ammo amount in IncreaseCurrentAmmo

In 8:38 can we just click on the Mesh Filter and select a different mesh so we don’t have to delete the cube?

Well, if you deleted the cube mesh and added a sphere instead, it wouldn’t be a cube anymore, would it? :smiley:
Also, if one confines all real functionality one can swap out the gameobject that does the graphical representation pretty easily to start with. Just throw one out, add another one, remove or disable extraneous collider if there’s one, and perhaps adjust the collider on the parent object, and you’re done.

To make a more fancy pickup, I used the Carbine prefab, and prefabbed a copy of its bullet magazine to use instead of a plain cube or other primitive…

Also, I implemented the pickup destruction with a delayed Coroutine, to give me a good hook to possibly add some VFX or SFX for the pickup to occur…
My script is actually checking on whether the other object has an Ammo component (before it also checks for the Player tag), so one could actually have enemies that could snatch pickups away…

My enemies seem to be missing a rigidbody that would trigger the pickup script, anyway…

For sure it is to late, but… I made it today (added only part of code that is changed)
Maybe it will be helpful for someone else … :sunglasses:

    private class AmmoSlot
    {
        public AmmoType ammoType;
        public int ammoAmount;
        public int maxAmmoAmount;
    }

    public int GetMaxAmmoAmmount(AmmoType ammoType){
        return GetAmmoSlot(ammoType).maxAmmoAmount;
    }

    public void IncreaseCurrentAmmo(AmmoType ammoType, int ammoAmount){
        GetAmmoSlot(ammoType).ammoAmount += ammoAmount;
        if(GetAmmoSlot(ammoType).ammoAmount > GetAmmoSlot(ammoType).maxAmmoAmount){
            GetAmmoSlot(ammoType).ammoAmount = GetAmmoSlot(ammoType).maxAmmoAmount;
        }
    }

in AmmoPickup:

    if(other.tag == "Player"){
        if(ammoSlot.GetAmmoAmmount(ammoType) < ammoSlot.GetMaxAmmoAmmount(ammoType))
        {
            ammoSlot.IncreaseCurrentAmmo(ammoType, ammoAmmount);
            DestroyAmmoPickup();
        }
    }

I’m not sure if this is version related or not, but using Unity 2022.3 and found that using FindObjectOfType
only worked for the original carbine. When using the other weapons it seemed to lose the reference to the relevant player ammo, and I got the error:

NullReferenceException: Object reference not set to an instance of an object Ammo.IncreaseCurrentAmmo

Using other.GetComponent() fixed this.

Has anyone else encountered this?

Privacy & Terms