- Home /
Display Ammo Count Above Player
Similar to Towerfall, I am trying to display a graphical ammo count above my player's head; but only when they grab a Power-Up. (I've attached an image of Towerfall for reference, note the Arrows above the player's heads).
I have a single "Weapons Script" that is controlling the projectiles and ammo counts for all Power-Up types.
What I'm seeking help with is displaying the appropriate number of ammo sprites above my player's head, and deactivating them one at a time as the player shoots.
I know a way to do this that is hard-coded, but since each Power-Up has different Ammo Counts, I'm looking for help finding a more dynamic solution.
If it helps, here is my Weapon script code:
public GameObject projectile;
public Transform nozzle;
public Transform nozzleTop;
public Transform nozzleBot;
public bool standardShot;
public bool scatterShot;
public bool homingShot;
public bool lobberShot;
public float fireRate;
private float shotTime;
public float ammoCount;
void Start()
{
if (scatterShot == true)
{
ammoCount = 3f;
}
if (lobberShot == true)
{
ammoCount = 5f;
}
}
void FixedUpdate()
{
if (standardShot == false && ammoCount == 0f)
{
SendMessageUpwards("DefaultWeapon");
}
}
void Shoot () {
//Standard Shot
if(Time.time >= shotTime && standardShot)
{
Instantiate(projectile, nozzle.position, nozzle.rotation);
FindObjectOfType<AudioManager>().Play("Shoot");
shotTime = Time.time + 1 / fireRate;
}
//Scatter Shot
if (Time.time >= shotTime && scatterShot)
{
Instantiate(projectile, nozzle.position, nozzle.rotation);
Instantiate(projectile, nozzleTop.position, nozzleTop.rotation);
Instantiate(projectile, nozzleBot.position, nozzleBot.rotation);
FindObjectOfType<AudioManager>().Play("weapon_ScatterShot");
ammoCount--;
shotTime = Time.time + 1 / fireRate;
}
//Lobber Shot
if (Time.time >= shotTime && lobberShot)
{
Instantiate(projectile, nozzle.position, nozzle.rotation);
FindObjectOfType<AudioManager>().Play("weapon_LobberShot");
ammoCount--;
shotTime = Time.time + 1 / fireRate;
}
//Homing Shot
if (Time.time >= shotTime && homingShot)
{
Instantiate(projectile, nozzle.position, nozzle.rotation);
FindObjectOfType<AudioManager>().Play("weapon_ScatterShot");
shotTime = Time.time + 1 / fireRate;
}
}
}
Thanks in advance! :D
Answer by Triggerly · Apr 23, 2020 at 03:15 PM
This is the simplest way I can think of.
You can make a text UI and set it as 'World Space' and make it above your sprite. Then make the text a child of the sprite so it follows it. Make public float ammoCount;
to public static float ammoCount;
and make another script that updates the ammoCount with the text UI.
Sorry if this doesn't work.
Your answer
Follow this Question
Related Questions
2D FPS shooting 1 Answer
UI Image has no sprite definition 0 Answers
Convert Input.mousePosition to RectTransform pivot position 2 Answers
2d bullet shot at bullet emitter 0 Answers
Prefab a 2D UI? 1 Answer