簡體   English   中英

敵人的健康欄並受到傷害

[英]Enemy health bar and taking damage

為了擁有健康欄,我向我的敵人添加了以下腳本:

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;
    }
}

它運行良好。 但是,我的玩家有一把武器(斧頭),我希望當我的玩家使用該武器進行攻擊時,敵人的生命值會減少。

添加降低敵人攻擊時生命值的功能。 像這樣的東西:

public void DecreaseHealth(healthamount)
{
    health -= healthamount;
}

在您的播放器腳本中調用此函數,並使用您希望在攻擊時降低的確切生命值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM