Multiple enemy health bars with 3 different sliders and a game manager script
hi in my team's project I have been tasked with creating health bars for the enemy, currently, the enemy takes damage from the player and the enemy can die I now need to create a health bar for them using the game manager script they all have individual health values but it is controlled under the same script need help with making health bars that reflect every enemy actually health I have canvases and bar objects for all enemies in the scene under the enemy prefab. This is the player movement script that does the attacking:
if (!Physics.Linecast(Player_Char.transform.position, hit.transform.position, ~(1 << 8)))
{
//checks if the selected enemy is in range
if (Vector3.Distance(Player_Char.transform.position, hit.transform.position) < gameObject.GetComponent<Player_Character>().Range)
{
Debug.Log("Hit");
//deal the player's damage to the enemy's current health
hit.transform.GetComponent<Enemy_Manager>().Current_Health -= Player_Char.GetComponent<Player_Character>().Damage;
Player_Char.GetComponent<Player_Character>().Action_Points -= 1;
//Update UI here
gm.UpdateAPUI(Player_Char.GetComponent<Player_Character>().Action_Points);
}
else
{
Debug.Log("Out of Range");
}
}
This is the enemy manager-script:
private void Start()
{
Max_Health = 20;
Current_Health = Max_Health;
//Game_Manager = GameObject.FindWithTag("Game_Manager");
//gm = Game_Manager.GetComponent<Game_Manager>();
Seen_By = 0;
}
private void Update()
{
//UI health update
//gm.UpdateAPUI(GetComponent<Enemy_Manager>().Current_Health);
//detects if it's dead and removes it if it is
if (Current_Health <= 0)
{
Seen_By = 0;
Destroy(gameObject);
Debug.Log("Dead");
}
lastly here is the game manager script code:
using UnityEngine.UI;
public class Game_Manager : MonoBehaviour
{
public TextMeshProUGUI txt;
public List<GameObject> Player_list;
//public Slider EneHealthslider1;
//public Slider EneHealthslider2;
//public Slider EneHealthslider3;
private int Random_Int;
private GameObject Temp_Char;
private GameObject Current_Char;
//private GameObject Enemy_1;
//private GameObject Enemy_2;
//private GameObject Enemy_3;
private int Current_Char_Pos;
// Start is called before the first frame update
void Start()
{
//makes a list of all player characters
Player_list.AddRange(GameObject.FindGameObjectsWithTag("Player"));
//randomizes the list of player characters
System.Random New_Random = new System.Random();
for (int i = 0; i < Player_list.Count; i++)
{
Random_Int = i + (int)(New_Random.NextDouble() * (Player_list.Count - i));
Temp_Char = Player_list[Random_Int];
Player_list[Random_Int] = Player_list[i];
Player_list[i] = Temp_Char;
}
Current_Char_Pos = 0;
Current_Char = Player_list[Current_Char_Pos];
Current_Char.GetComponent<Player_Character>().End_turn();
UpdateAPUI(Current_Char.GetComponent<Player_Character>().Action_Points);
//UpdateEnemyHealthUI1(Enemy_1.GetComponent<Enemy_Manager>().Current_Health);
// UpdateEnemyHealthUI2(Enemy_2.GetComponent<Enemy_Manager>().Current_Health);
//UpdateEnemyHealthUI3(Enemy_3.GetComponent<Enemy_Manager>().Current_Health);
}
//is called when the end turn button is pressed, disables control of the current character and enables it for the next
public void Turn_End()
{
Current_Char.GetComponent<Player_Character>().End_turn();
Current_Char_Pos += 1;
if (Current_Char_Pos > Player_list.Count -1)
{
Current_Char_Pos = 0;
}
Current_Char = Player_list[Current_Char_Pos];
Current_Char.GetComponent<Player_Character>().End_turn();
UpdateAPUI(Current_Char.GetComponent<Player_Character>().Action_Points);
}
public void UpdateAPUI(int APvalue)
{
txt.text = APvalue.ToString();
}
//public void UpdateEnemyHealthUI1(int EneHealthvalue1)
//{
//EneHealthslider1.Slider = EneHealthvalue1.float;
//}
//public void UpdateEnemyHealthUI2(int EneHealthvalue2)
// {
// }
//public void UpdateEnemyHealthUI3(int EneHealthvalue3)
// {
//}
Your answer
Follow this Question
Related Questions
how to enter from one stage to other in road ?? 1 Answer
How to make this shop animation and unlock system? 0 Answers
Change Value Of UI Light Slider to Read At Specific Points Of Slider Value 1 Answer
How do I apply a mask to a slider fill so it uses my quadrilateral design 0 Answers
Detect if onValueChanged() of slider is done manually or via script 0 Answers