[英]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.