[英]Enemy health bar and taking damage
I have added the following script to my enemies for the purpose of having a health bar:为了拥有健康栏,我向我的敌人添加了以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy_Health : MonoBehaviour {
public float health;
public float maxHealth;
public GameObject healthBarUI;
public Slider slider;
private void Start()
{
health = maxHealth;
slider.value = calculateHealth();
}
private void Update()
{
slider.value = calculateHealth();
if (health < maxHealth)
{
healthBarUI.SetActive(true);
}
if (health <= 0)
{
Destroy(gameObject);
}
if (health > maxHealth)
{
health = maxHealth;
}
}
float calculateHealth()
{
return health / maxHealth;
}
}
It is working well.它运行良好。 However, my player has a weapon (axe) and I would like when my player attacks using the weapon, the health bar of the enemy decreases.
但是,我的玩家有一把武器(斧头),我希望当我的玩家使用该武器进行攻击时,敌人的生命值会减少。
Add a function that decreases the health of enemy on attack.添加降低敌人攻击时生命值的功能。 Something like this:
像这样的东西:
public void DecreaseHealth(healthamount)
{
health -= healthamount;
}
Call this function in your player script with the exact health you want to decrease on attack.在您的播放器脚本中调用此函数,并使用您希望在攻击时降低的确切生命值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.