简体   繁体   中英

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:

  • The text mesh is drawn behind the bar. This is because GUI rendering is done after scene rendering .
  • The positions for the bar and text are different. The bar is on top of the opponent's head because you add the height to its position. But the text remains at the feet. Perhaps the text is behind something or outside of the screen?
  • The text mesh does not rotate to face the camera. With the default font material you can see it from the back, but depending on the material you might see nothing if viewed from the back. Since the mesh has no depth you won't see it regardless of material when viewed from the side.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM