[英]Displaying enemy health above enemy [works, but not really]?
I'm working on a health bar system that shows a heath bar that follows the enemy wherever they go [which works perfectly]. 我正在开发一个健康栏系统,该系统可以显示一条健康路线,该路线可以跟随敌人所到之处[完美运行]。
But I also want to have the health displayed in numbers on the health bar as well.. which I can't get to work. 但是我也想在健康栏上以数字显示健康状况。..我无法上班。 There are no errors, but it just doesn't show up on the screen.
没有错误,但只是没有显示在屏幕上。
Here's my health bar class: 这是我的健康吧课程:
//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 what I'm messing with to show how much HP the enemy has (I separated this code into another section so it's easier to understand, normally I just try and add it to the above script. 这就是我要弄清楚敌人有多少生命(我将这段代码分为另一部分,以便于理解),通常我只是尝试将其添加到上面的脚本中。
//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();
}
You are using different systems for the bar and the text. 您正在为栏和文本使用不同的系统。 Possible causes why this isn't working:
导致此无效的可能原因:
Probably the best way to fix this is to draw the text using a label with a transparent background in OnGUI() right after you draw the bar. 解决此问题的最佳方法是在绘制条形之后立即在OnGUI()中使用带有透明背景的标签绘制文本。 This will ensure that the positions match up and that the text is in front of the bar.
这将确保位置匹配,并且文本在栏的前面。
Also check in the editor if your textmesh shows up anywhere. 还要在编辑器中签入您的文本网格是否显示在任何地方。 See if there's anything wrong with it.
看看是否有任何问题。 Maybe the wrong material is assigned, maybe the position is off.
也许分配了错误的物料,或者仓位已关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.