Project Bölümünden Create > C# Script yapıyoruz ve içindekileri silip aşağıdaki kodları yapıştırıyoruz;
Son olarak bu scripti karakterimize atmamız yeterli oluyor play tuşuna bastığınızda sol tarafda can barınızı görebileceksiniz.
Kod:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
**** Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
**** Update () {
AddjustCurrentHealth(0);
}
**** OnGUI() {
GUI.Box(new Rect(10, 10, healthBarLength, 20 ), curHealth + "/" + maxHealth);
}
public **** AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Son olarak bu scripti karakterimize atmamız yeterli oluyor play tuşuna bastığınızda sol tarafda can barınızı görebileceksiniz.