[英]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();
}
您正在為欄和文本使用不同的系統。 導致此無效的可能原因:
解決此問題的最佳方法是在繪制條形之后立即在OnGUI()中使用帶有透明背景的標簽繪制文本。 這將確保位置匹配,並且文本在欄的前面。
還要在編輯器中簽入您的文本網格是否顯示在任何地方。 看看是否有任何問題。 也許分配了錯誤的物料,或者倉位已關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.