Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity health bar

//SCRIPT HealthBar.CS to put on your HealthBar Inspector

using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
  public Image healthBarImage;
  public Player player;
  public void UpdateHealthBar() {
    healthBarImage.fillAmount = Mathf.Clamp(player.health / player.maxHealth, 0, 1f);
  }
}
//____________________________________________________________________________
//SCRIPT Player.CS to put on your Player Inspector

using UnityEngine;
using UnityEngine.UI;
public class Player: MonoBehaviour
{
  public float health, maxHealth;
  public HealthBar healthBar;
  public void TakeDamage(){
    // Use your own damage handling code, or this example one.
    health -= 25;            
    healthBar.UpdateHealthBar();
  }
  void Update(){
    // Example so we can test the Health Bar functionality
    if(Input.GetKeyDown(KeyCode.Space)){
      TakeDamage();
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Operator Overloading | C# 
Csharp :: c# mongodb update multiple fields 
Csharp :: rock paper scissors c# 
Csharp :: split string on last element 
Csharp :: how to run async void function c# 
Csharp :: array sorting c# 
Csharp :: change image of button c# 
Csharp :: get tree node godot 
Csharp :: c# enum default 
Csharp :: unity editor script 
Csharp :: how to create a list c# 
Csharp :: build cs file 
Csharp :: single line and multiline comments in c# 
Csharp :: how use unity interfaces 
Csharp :: calculate how much memory an object take c# 
Csharp :: c# radio button checked 
Csharp :: aspx import namespace 
Csharp :: how to check type c# 
Csharp :: wpf toolbar disable overflow 
Csharp :: unity class 
Csharp :: sort file name with C# 
Csharp :: unity c# struct 
Csharp :: append multi lines to file linux 
Csharp :: c# swap name in string 
Csharp :: c# #region #endregion 
Csharp :: c# read all lines from filestream 
Csharp :: How to post request C# with returning responsebody 
Csharp :: unity unfreeze position in script 
Csharp :: unity making homing missile 
Csharp :: unity2d movement 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =