I did a floating Health Bar (using canvas UI) that attach itself on an object and always rotate facing the main camera.
All works well but I’ve got a problem: if the object start rotate on Z/X axys the canvas still be on the Y axis of the object.
I try to explain: if the object is a sphere and I attach a UI canvas on it I’ll see the canvas above the sphere.
If the sphere rotate on the Y axis (horizontal rotation) all is ok (che canvas is still above the sphere) but if the object start rotating on the X or Z axis (making a vertical rotation) the canvas that is attached to the Y axis rotate with the sphere.
If you want a health bar above an enemy (or something else in the game), you first take position of enemy, then add some Y value, so it’s a bit above the enemy (for example).
Then, you take that position above the enemy, and you recalculate it from 3D world position, into 2D Screen position. You can use this function for such a calculation:
Then, you make a healthbar object and position it on the 2D screen position of your main HUD canvas.
That is how I would do it, at least.
There is also alternative way, if you just want to fix your current problem quickly:
Make a script that every enemy will have. Script must find the Healthbar object that is attached to the enemy. Then, it will simply position the healthbar object above the enemy, every frame. Can you do that, or do you need help with code?
So you suggest instead of having a Canvas attached on each object to have a picture (representing the healthbar) in the main HUD canvas and position it with the camera.WorldToScreenPoint?
Nice idea, I’ve to try it!
About the second solution: so instead of having a canvas attached to the object I position the canvas above it every frame? it could be good but the main HUD canvas seems to be a better solution