我正在开发一个健康栏系统,显示一个健康棒,无论走到哪里都能跟随敌人[完美无缺]。
但我也希望健康栏上的健康状况显示健康状况......我无法开始工作。没有错误,但它只是没有显示在屏幕上。
这是我的健康酒吧课程:
//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);
}
}
这就是我要弄清楚敌人有多少HP(我将这段代码分成另一部分所以它更容易理解,通常我只是尝试将其添加到上面的脚本中。
//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();
}
答案 0 :(得分:0)
您正在为条形图和文本使用不同的系统。可能导致这种情况无效的原因:
解决此问题的最佳方法可能是在绘制条形图后立即使用OnGUI()中的透明背景使用a label绘制文本。这将确保位置匹配并且文本位于栏前面。
如果你的textmesh显示在任何地方,也请检查编辑器。看看它是否有任何问题。可能分配了错误的材料,可能是位置关闭。