Hi there, I'm pretty new to Unity.
I was trying to make a lifebar for my player with Unity's GUI through code in C#.
The maximun life is 200 and the minimum 0. I've already got the damage sorted out practically, but I wanted to display it in a simple lifebar.
I want to find a way to make a GUI bar in the top-left side of the screen that is represented as CurrentLife/TotalLife (TotalLife should be the max size, and should have to be another GUI as a background texture).
There should be a string "LIFE" in the middle of the bar.
Also, I want to make the bar and the string to shake when a bullet hits the player.
If you could help me, I'd also like to make a label that shows how many enemies I have killed (I use the Destroy.GameObject thing) in the top right corner of the screen. And, as a player is killed, there should be a Points Label that increases by 20 everytime I kill an enemy (as in "SCORE: 0", I kill an enemy "SCORE: 20", another, "SCORE: 40").
And, to end this, I wanted to add an "ACCURACY" label under the above (as in ShotsThatHitAnEnemy/ShotsMade).
If anyone could help me in any of this, It'd be much appreciated.
Thanks in advance! :)
Okay, I've been coding a little bit and this is what I got
using UnityEngine;
using System.Collections;
public class UserInterfaceGraphics : MonoBehaviour {
public Rect lifeBarRect;
public Rect lifeBarLabelRect;
public Rect lifeBarBackgroundRect;
public Texture2D lifeBarBackground;
public Texture2D lifeBar;
//private float damage = 10;
private PlayerVitalData PVDScript;
public GameObject PlayerData;
private float LifeBarWidth = 300f;
// Use this for initialization
void Start(){
instance = this;
PVDScript = PlayerData.GetComponent("PlayerVitalData") as PlayerVitalData;
}
// Update is called once per frame
public static UserInterfaceGraphics instance;
void OnGUI()
{
instance.lifeBarRect.width = LifeBarWidth * (PVDScript.life/200);
instance.lifeBarRect.height = 20;
instance.lifeBarBackgroundRect.width = LifeBarWidth;
instance.lifeBarBackgroundRect.height= 20;
GUI.DrawTexture(lifeBarRect, lifeBar);
GUI.DrawTexture(lifeBarBackgroundRect, lifeBarBackground);
GUI.Label(lifeBarLabelRect, "LIFE");
}
}
The Life Bar or Health Bar or whatever now works like wonders. I just got hurried up and made questions too soon, instead of searching before.
I hope it helps anyone down the road.
Use GUI.Box instead. Define the lenght of the box using the amount of life left. You have to call the variables from your health script. You have to make assign your texture in the new style.
For example:
using UnityEngine;
using System.Collections;
public class UserInterfaceGraphics : MonoBehaviour {
public float currentLife;
public GUIStyle : myGUIStyle;
Void OnGUI () {
//For example you have 100 life’s maximum.
GUI.Box(new Rec(10, 10, 0.001 * Screen.width * currentlife, 0.1 * Screen.heigth), “LIFE”, myGUIStyle);
}
}
I hope this is helpfull.