簡體   English   中英

如何檢查調試? 為什么我的播放器健康無法正常工作?

[英]how can I check for debug? why my playerhealth is not working?

如何調試setHealth方法以查明是否正在調用它? 播放器運行狀況腳本不起作用? 播放器的健康腳本應遞減健康狀態,並且健康欄應減少。 我想知道應該在哪里調試以及如何在代碼中調試以查看被調用的內容? 目前,當我玩游戲時,沒有語法錯誤,但運行狀況欄沒有減少,玩家游戲對象立即死亡。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerHealth : MonoBehaviour
{
    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;
    public Image Bar;
    public Text Text;
    public float max_health = 100f;
    public float cur_health = 0f;


    //Use this for initialization
    void Start()
    {
        // Initialize the health that is given
        cur_health = max_health;
        InvokeRepeating("decreaseHealth", 0f, 2f);
    }

    void Update()
    {
        if (cur_health <= 0)
        {
            Destroy(gameObject);   //if the player has no health point left, destroy the player.
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.GetComponent<Projectiles>())
        {
            max_health -= cur_health;
            //if the collision object has a homing script, minus player health by   damageToPlayer
        }
    }

    void decreaseHealth()
    {
        //Subtract the health at the following rate
        //Check if the health is 0 before we do any damage
        if (cur_health < 0)
        {
            cur_health = 0;
        }
        //make a new variable and divide the current health my the maximum health
        //this is because the fill value goes from 0 to 1
        float calc_health = cur_health / max_health; // 70 / 100 = 0.7
        SetHealth(calc_health);

        //Change the color of the health bar
        // on the scale of 1000, if health <= 625 and is greater    than 345, do the following
        if (cur_health != 0 && cur_health <= max_health / 1.6 && cur_health >    max_health / 2.9) 
        {
            Bar.color = new Color32(171, 162, 53, 255);
        }
        else if (cur_health != 0 && cur_health <= max_health / 2.9) // on the    scale of 1000, if health <= 625, do the following
        {
            Bar.color = new Color32(158, 25, 25, 255);
        }
    }

    void SetHealth(float myHealth)
    {
        //defill the bar based on the current health
        Bar.fillAmount = myHealth;

        //change the text to display the amount of health
        Text.text = cur_health.ToString("f0") + "/100";
    }
}

您的錯誤來自以下事實:您正在減少MaxHealth而不是curHealth(在OnCollisionEnter中)。 這些是下一次的一些提示。

縮進代碼

在發布問題時以及在腳本中閱讀起來都更容易。

使用常數

不應隨時間變化的變量應聲明為const

public const float max_health = 100f;

這樣,您的編譯器將告訴您有關錯誤的信息。

如果在程序生命周期中,最大運行狀況可能根據實例化類的方式而改變,則還可以使用readonly關鍵字:

public readonly float max_health;

該值只能在類構造函數中設置,然后就不能再更改。

使用調試器

使用調試工具可以輕松發現此錯誤。 查看有關您的開發環境的文檔。

暫無
暫無

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

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