16.6 C
New York
Monday, June 16, 2025

Find out how to place well being bars above models in Unity no matter digital camera location, angle and zoom?


Here’s what I need to obtain, well being bars that keep completely above the models, no matter the place the unit strikes or the place the digital camera is: https://www.youtube.com/stay/fZSaPDz-GJc?si=B868wwF5KAPCuEtw&t=3305

I’ve tried all type of issues, each having my canvas in world-space and screen-space.

In world-space I can’t get my canvas to correctly keep in place above the unit, whereas all the time going through the digital camera.

rework.rotation = Quaternion.LookRotation(rework.place - Digicam.primary.rework.place);

I additionally tried with out – Digicam.primary.rework.place and likewise with + as a substitute, nothing has the specified outcome and I can not fairly wrap my mind round how all of this works.
One other factor I attempted is that this, each with and with out the y freeze.

Vector3 ahead = _cam.rework.ahead;
ahead.y = 0;
ahead.Normalize();
healthbarCanvas.rework.ahead = ahead;

This makes the sides of my well being bar go up/down because the digital camera strikes, it would not keep horizontal prefer it ought to.

In display area I bought fairly shut, but it surely’s nonetheless transferring a bit once I’m transferring my digital camera and the well being bar additionally will get greater/smaller as I zoom in/out.

public override void OnLateUpdateView()
{
    Vector3 offset = new Vector3(0, 3.5f, 0);
    Vector3 worldPos = rework.place + offset;
    Vector3 screenPos = _cam.WorldToScreenPoint(worldPos);

    healthbar.rework.place = screenPos;
}

I’ve tried a number of variations, like freezing the y rotation, freezing the z rotation, however nothing works precisely as I would like it to work.

I need well being bars that keep precisely above my unit because it strikes round in 3D area, no matter digital camera place, angle or zoom. Precisely just like the well being bars in League of Legends above the champions and minions work.

As steered I attempted:

rework.rotation = Quaternion.LookRotation(digital camera.ahead, digital camera.up);

Which ends up in this:
From far:

From close by:
from naerby

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles