I'm trying to display some Particles and Slider (like a health bar) over a GameObject in Unity 2020.3.26, but even if in the Scene it's showing like I want, when playing, the canvas seems to orient itself "too much" and I don't know how to make it so both the Particles and Slider stays in place.
As shown in the images, I've put a canvas in a GameObject "Aimable Target Body", and when I look at the center of the object it works as I want. But if I start looking away, instead of staying centered, the UI goes a little away (don't mind the big blue magic circle in the middle, it's supposed to be there).
I don't think I have a problem with my Canvas settings (I make it so it's always facing the camera) but I don't see where else it could go wrong...
Thanks in advance for your answers !
CodePudding user response:
The main reason for this is that the target exists in the world, while the slider exists on the canvas. Those two elements have different coordinate systems, and if you want the center of the Circle Slider to be at the center of the Sphere, you have to project one set of coordinates to another one (World to Screen).
This can be done using the WorldToScreenPoint method on Camera.
Basically, the UI elements (existing on Canvas) will have to check the transform.position
of the sphere in the world in Update
function, and set their position using _mainCamera.WorldToScreenPoint(sphereTransform.position)