HealthBar.cs
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public あなたのHPスクリプト あなたのHPスクリプト;
public Image healthBar;
public float 変化速度 = 3f;
void Update()
{
if (あなたのHPスクリプト == null)
return;
if (healthBar == null)
return;
float 割合 =
あなたのHPスクリプト.あなたのHP /
あなたのHPスクリプト.あなたの最大HP;
healthBar.fillAmount =
Mathf.Lerp(
healthBar.fillAmount,
割合,
変化速度 * Time.deltaTime
);
}
}
HealthBarColor.cs
using UnityEngine;
using UnityEngine.UI;
public class HealthBarColor : MonoBehaviour
{
public あなたのHPスクリプト あなたのHPスクリプト;
public Image healthBar;
public float 変化速度 = 3f;
void Update()
{
if (あなたのHPスクリプト == null)
return;
if (healthBar == null)
return;
float 割合 =
あなたのHPスクリプト.あなたのHP /
あなたのHPスクリプト.あなたの最大HP;
healthBar.fillAmount =
Mathf.Lerp(
healthBar.fillAmount,
割合,
変化速度 * Time.deltaTime
);
healthBar.color =
Color.Lerp(
Color.red,
Color.green,
割合
);
}
}
HealthPoint.cs
using UnityEngine;
using UnityEngine.UI;
public class HealthPoint : MonoBehaviour
{
public あなたのHPスクリプト あなたのHPスクリプト;
public Image[] healthPoints;
void Update()
{
if (あなたのHPスクリプト == null)
return;
if (healthPoints == null || healthPoints.Length == 0)
return;
float ポイント1個分のHP =
あなたのHPスクリプト.あなたの最大HP /
healthPoints.Length;
for (int i = 0; i < healthPoints.Length; i++)
{
if (healthPoints[i] == null)
continue;
healthPoints[i].enabled =
(i + 1) * ポイント1個分のHP <=
あなたのHPスクリプト.あなたのHP;
}
}
}
あなたのHPスクリプト.cs
using UnityEngine;
public class あなたのHPスクリプト : MonoBehaviour
{
public float あなたの最大HP = 100;
public float あなたのHP = 100;
public void Damage(float damage)
{
あなたのHP -= damage;
if (あなたのHP < 0)
あなたのHP = 0;
}
public void Heal(float heal)
{
あなたのHP += heal;
if (あなたのHP > あなたの最大HP)
あなたのHP = あなたの最大HP;
}
}


コメント