Adding ui healthbar to each enemy

Trying to use the example pinned all the health bar goes in a corner, i suspect i need to set the canvas or camera someway but no clue how, anyone can help?

what example? pinned where?

how have you implemented the health bar?

can’t tell you where you’ve gone wrong without knowing what you’ve done.

Something like this should be able to get you started:

// Calculate the enemy's height by using its capsule collider (we perform this every frame in case the enemy grows/shrinks during play)
float enemyHeight = enemy.GetComponent<CapsuleCollider>( ).height * enemy.transform.localScale.y;

// Create a vector representing the top of the enemy's head
Vector3 topOfUnit = new Vector3( enemy.transform.position.x, enemy.transform.position.y + enemyHeight, enemy.transform.position.z );

// Convert the vector to a point on screen
Vector3 enemyScreenPos = Camera.main.WorldToScreenPoint( topOfUnit );

// If the enemy is behind the camera, or too far away from the player, make sure to hide the health bar completely
if ( enemyScreenPos.z < 0 || Vector3.Distance( enemy.transform.position, player.transform.position ) > maxDrawDistance )
    panel.gameObject.SetActive( false );
else
{
    // Move the panel to the top of the enemy's head
    panel.position = enemyScreenPos;

    panel.gameObject.SetActive( true );
}

Where “panel” is just a reference to a basic UI panel that is already located somewhere within a canvas.

1 Like

ty i was using world to viewport for some reason now it works