using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CharacterStats : MonoBehaviour
{
private CharacterController characterController { get { return GetComponent<CharacterController>(); } set { characterController = value; } }
private RagdollManager ragdollManager { get { return GetComponentInChildren<RagdollManager>(); } set { ragdollManager = value; } }
public PlayerUI UI { get { return GetComponent<PlayerUI>(); } set { UI = value; } }
private CharacterStats stats;
private Weapon wp { get { return GetComponent<Weapon>(); } set { wp = value; } }
private GameController gc{get{return GetComponent<GameController>();} set {gc = value;}}
[Range(0, 100)]
public int CurrentHealth = 100;
public int Highheal = 100;
public int Midheal = 50;
public int Lowheal = 25;
public int MaxHealth = 100;
public int faction;
public MonoBehaviour[] scriptsToDisable;
public Image damageImage;
public Image healImage;
public float healFlashSpeed = 10f;
public Color healFlashColor = new Color(1f, 0f, 0f, 1f);
public float flashSpeed = 5f;
public Color flashColor = new Color(1f, 0f, 0f, 0.5f);
bool damage;
bool heal;
public Color LowHealth = Color.red;
public Color FullHealth = Color.green;
void Awake()
{
CurrentHealth = MaxHealth;
CurrentHealth = Mathf.Clamp(CurrentHealth, 0, 100);
}
void Start()
{
}
void Update()
{
if (gameObject.tag == "Player")
{
if (heal)
{
healImage.color = healFlashColor;
}
else
{
healImage.color = Color.Lerp(healImage.color, Color.clear, healFlashSpeed * Time.deltaTime);
}
heal = false;
}
if (gameObject.tag == "Player")
{
if (damage)
{
damageImage.color = flashColor;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damage = false;
}
healthColorChange();
}
public void Damage(int amount)
{
damage = true;
CurrentHealth -= amount;
if (CurrentHealth <= 0)
{
Die();
}
}
public void Die()
{
characterController.enabled = false;
if (scriptsToDisable.Length == 0)
{
return;
}
foreach (MonoBehaviour script in scriptsToDisable)
script.enabled = false;
if (ragdollManager != null)
ragdollManager.RagDoll();
if(gameObject.CompareTag("Enemy"))
{
Destroy(gameObject, Random.Range(4f, 5f));
}
if(gameObject.tag == "Player")
{
if(CurrentHealth <=0)
{
gc.LastCheckPointPos = transform.position;
}
}
}
public void HighHeal()
{
if(gameObject.tag == "Player")
{
if (CurrentHealth > 100)
return;
if(CurrentHealth < 100)
CurrentHealth = Highheal;
heal = true;
}
}
public void MidHeal()
{
if(gameObject.tag == "Player")
{
CurrentHealth += Midheal;
heal = true;
}
}
public void LowHeal()
{
if(gameObject.tag == "Player")
{
CurrentHealth += Lowheal;
heal = true;
}
}
public void healthColorChange()
{
if(gameObject.tag == "Player")
{
UI.HealthText.color = Color.Lerp(FullHealth, LowHealth, CurrentHealth);
}
}
}
then this is my player UI
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour
{
public Text ammoText;
public Text HealthText;
}