how to make a health bar??as it decreases when any enemy hit and increaes when i take any power?
You can refer the Unity FPS tutroial itself…It deals with health bar and many things…
Unity FPS Tutorial
And video tutorial is also available in youtube, where it is explained very clearly…
YouTube Link for FPS Step by Step
Try this
- Go to GameObject | Create Empty.
- Rename the empty GameObject to healthBar.
- Create a new JavaScript and name it healthScript.
- Drag-and-drop the healthScript onto the healthBar Game Object.
- import file attachment to your Asset
- select healthBar Game Object, look Health Script in the Inspector
- drag and drop clockBG and clockFG from your Asset onto the Health Script
Play it and try to click your left mouse
/*Health Script*/
var clockBG : Texture2D;
var clockFG : Texture2D;
var clockFGMaxWidth : float; //the starting width of the foreground bar
var currentHealth:float;
function Start()
{
startTime = 10.0;
clockFGMaxWidth = clockFG.width;
currentHealth = 100;
}
function Update ()
{
if (Input.GetButtonDown("Fire1"))
currentHealth -= 1;
}
function OnGUI()
{
//this is the width that the foreground bar should be
var newBarWidth:float = (currentHealth/100) * clockFGMaxWidth;
Debug.Log("current health " + currentHealth);
//a spacing variable to help us position the clock
var gap:int = 20;
GUI.BeginGroup(new Rect (Screen.width - clockBG.width - gap,
gap, clockBG.width, clockBG.height));
GUI.DrawTexture(Rect (0,0, clockBG.width, clockBG.height), clockBG);
GUI.BeginGroup(new Rect(5,6, newBarWidth, clockFG.height));
GUI.DrawTexture(Rect(0,0, clockFG.width, clockFG.height), clockFG);
GUI.EndGroup();
GUI.EndGroup();
}
but sir i want it to take away when he is hit by bomb not when he shoot or hit click w.e.
why would u make it this way?
there is a free health bar asset on the asset store