how to create a UI hovering over target

When a NavMeshAgent is damage, a health bar is created as a child in the main Canvas that uses screen space overlay.

The code I’m using can the set position of the UI to very high values when looking in certain directions. And, shows the UI when looking behind.

Vector3 UIoffset = new Vector3(0, 1, 0);
ThisRect.position = PlayerCamera.Cam.WorldToScreenPoint(DroneTransform.position + UIoffset);

8953074--1229295--ezgif-4-db605afddd.gif

What is the correct way of displaying health bars? (like in elden ring).

Take the dot product between the camera’s forward vector and the vector from → to the item.

If the dot is positive, it is in front of you.

If the dot is negative, it is behind you.

1 Like

That solved it.

Vector3 Foward = PlayerCamera.Cam.transform.forward;
Vector3 DronePosition =  DroneTransform.position - PlayerCamera.Cam.transform.position;
ThisGroup.alpha = Vector3.Dot(Foward, DronePosition) > 0 ? 1 : 0;
1 Like