Expanding on health bar rotation

Hi, I just finished lesson 28 Enemy Floating Health Bar and was encouraged to share my differences in the discussion forum. I haven’t done anything different with the art and layout (those are not really my strengths), but I have made some slight alteration to the healthbar’s relative positioning.

So the healthbars are shown above the enemy’s head like a physical object, but pointed toward the camera. That’s great, but it’s not the effect I want. For the effect I want, the healthbars should be positioned above the apparent image of the enemy, regardless of its orientation, as if it were an overlay canvas.

The enemy could be summersaulting or dancing on its head, and I still don’t want the healthbar’s position to change unless the enemy’s position does. The other side of this is that I want the healthbar to appear above the image of the enemy, regardless of what perspective I point it at the camera. As it is now, if I look down from directly overhead, the healthbar will be in front of the enemy. This is because, as it is now, the healthbar will always be above the enemy’s head rather than above the enemy’s body or whole image.

Fixing this involved three steps:

  1. Instantiating using transform.rotation instead of Quaternion identity. (This was a great tip snatched from the Q&A, so that the rotation of the healthbars matched the rotation of the canvas (which, in turn, matches the rotation of the camera).

  2. I made sure that the healthbar components (including the canvas) were positioned at (0,0,0) and that the Socket UI was positioned in the center of the model. This will cause the healthbar to appear right in the middle of the model.

  3. Using the inspector (because it’s too small to just drag), I expanded the size of the canvas and positioned the healthbar at the very top, anchoring it to the top.

So now we have an invisible canvas that swivels around the center of the model, oriented toward the camera, and a healthbar that swings around on the outer edge of that canvas away from the model.

Here’s a picture:

You’ll notice in the second picture that the healthbar is not staying directly above the enemy’s head anymore, but has moved as the canvas faces the camera.

4 Likes

Was just about to ask for this as I much prefer the health bar always facing the player as well, so appreciate you sharing.

Thanks for the tip!

This is really great. I messed around with the canvas for a long time, as I constantly got the feeling that something was just wrong with the way it rotated, but couldn’t find a way to make it look “right.” I tried your solution and it looks perfect.

Took a bit of playing around but got it working thank you this is much better

nice one! “borrowed” :slight_smile: high5!

Privacy & Terms