簡體   English   中英

在敵人之上顯示敵人的健康[有效,但並非如此]?

[英]Displaying enemy health above enemy [works, but not really]?

我正在開發一個健康欄系統,該系統可以顯示一條健康路線,該路線可以跟隨敵人所到之處[完美運行]。

但是我也想在健康欄上以數字顯示健康狀況。..我無法上班。 沒有錯誤,但只是沒有顯示在屏幕上。

這是我的健康吧課程:

//This script works great.
public Texture2D healthBar;
public Texture2D healthBarFrame;

public float maxHealth;
public float curHealth;

private int healthBarWidth = 50;
private int healthBarHeight = 5;

private float left;
private float top;

private Vector3 healthBarScreenPosition;

public PlayerCombat player;
public Enemy target;
public float healthPercent;

void Start () {

}

void Update () {
    if(player.opponent != null) {
        target = player.opponent.GetComponent<Enemy>();
        healthPercent = (float)target.curHealth / (float)target.maxHealth;

        Vector3 healthBarWorldPosition = (target.transform.position + new Vector3(0.0f, target.transform.lossyScale.y, 0.0f));
        healthBarScreenPosition = Camera.main.WorldToScreenPoint(healthBarWorldPosition);
        left = healthBarScreenPosition.x - (healthBarWidth / 2);
        top = Screen.height - (healthBarScreenPosition.y + (healthBarHeight / 2));
    } else {
        target = null;
        healthPercent = 0;
    }
}

void OnGUI() {
    if (target != null) {
        GUI.DrawTexture(new Rect(left, top, 50, 5), healthBarFrame);
        GUI.DrawTexture(new Rect(left, top, (50 * healthPercent), 5), healthBar);
    }
}

這就是我要弄清楚敵人有多少生命(我將這段代碼分為另一部分,以便於理解),通常我只是嘗試將其添加到上面的腳本中。

//This is generic code taken from another user (with the variables unchanged)
public Font font;
public int fontSize = 8;
public Vector3 Offset = Vector3.zero; // The offset from the character

public Enemy target;
public PlayerCombat player;

private float health;

private TextMesh bar;

void Start()
{
    target = player.opponent.GetComponent<Enemy>();
    // Setup the text mesh
    bar = new GameObject("HealthBar").AddComponent("TextMesh") as TextMesh;
    bar.gameObject.AddComponent("MeshRenderer");
    bar.gameObject.transform.parent = transform;
    bar.transform.position = target.transform.position;

    if(font) bar.font = font;
    else bar.font = GUI.skin.font;
    bar.renderer.material = font.material;
    bar.characterSize = 0.25f;
    bar.alignment = TextAlignment.Center;
    bar.anchor = TextAnchor.MiddleCenter;
    bar.fontSize = fontSize;
}

void Update()
{
    target = player.opponent.GetComponent<Enemy>();
    health = target.curHealth;
    if(bar.text != "HP:" + health.ToString()) bar.text = "HP: " + health.ToString();
}

您正在為欄和文本使用不同的系統。 導致此無效的可能原因:

  • 文本網格被繪制在欄的后面。 這是因為GUI渲染是在場景渲染之后完成的
  • 條形圖和文本的位置不同。 該條形圖位於對手頭部的頂部,因為您將高度增加到其位置。 但案文仍在腳下。 文字可能在屏幕后面還是屏幕之外?
  • 文本網格不會旋轉以面向相機。 使用默認字體材料,您可以從背面看到它,但是根據材料的不同,如果從背面看,您可能看不到任何東西。 由於網格沒有深度,因此從側面看時無論材質如何,都不會看到它。

解決此問題的最佳方法是在繪制條形之后立即在OnGUI()中使用帶有透明背景的標簽繪制文本。 這將確保位置匹配,並且文本在欄的前面。

還要在編輯器中簽入您的文本網格是否顯示在任何地方。 看看是否有任何問題。 也許分配了錯誤的物料,或者倉位已關閉。

暫無
暫無

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

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