This content originally appeared on Level Up Coding - Medium and was authored by Christopher Adams
Creating Ammo Count and an Ammo Powerup for Your Game in Unity
Overview:
Today we are going over creating an ammo count and the ammo pickup.
Ammo Count:
Currently our player has infinite shots. We are going to limit the player to only have 15 shots max. Let’s get started.
- We create two private int variables for our max and current ammo.
2. We set the current ammo to the max ammo inside the start method.
3. Inside the shooting method, we update the ammo and check if our current ammo is greater than zero. if it is then we take one from our current ammo and update the UI. We set the fire rate to 0.3f, spawn a laser projectile, then set a bool can fire to false and start the laser Cool Down Coroutine.
4. We also need to create a new public method called Activate Reload. We set the current ammo equal to our max ammo and update the ui.
Ammo PickUp:
We are going to implement an ammo pick up so our player can reload. Let’s get started.
- I have a simple UI manager script attached to the canvas for updating the UI.
2. I have a power-up script for handling multiple power-ups, the variables are below. We have an audio Clip for the sound, a float for the movement speed, a power up ID, and a bool called can fire.
4. Inside the update method, I have a simple check if can move is true we move the power up down and once its off screen we destroy it.
5.We also create an OnTriggerEnter2D method. We check if the power-up collided with the player. Then we get the player script component off the Collided object and play a sound. We also check if the player script isn’t null. Then we run a switch statement depending on what ID is set on the power up inside the inspector. For the ammo, it should be four.
We should now have a pickup that will refill the player’s ammo.
That’s it for this article.
If you enjoyed it, clap and follow for more Unity content.
Thank you for your time and attention.
Creating Ammo Count and an Ammo Powerup for Your Game was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Christopher Adams
Christopher Adams | Sciencx (2022-09-06T11:42:52+00:00) Creating Ammo Count and an Ammo Powerup for Your Game. Retrieved from https://www.scien.cx/2022/09/06/creating-ammo-count-and-an-ammo-powerup-for-your-game/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.