D
D
dsadq1d12022-01-24 19:14:35
Unity
dsadq1d1, 2022-01-24 19:14:35

How to attach UI object to gameobject?

Good afternoon! I have a UI object which is a Slider I made an HPBar out of it. There are enemies, but ordinary 2D objects, how can I attach my UI element to them? I need it to be a little higher above the head of the enemies. I wanted to make the HPBar a child of my enemies so that they move together, but I ran into a problem, if I transfer my UI element to a child of the normal one, then it stops showing, because. I realized that it is not in canvas. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2022-01-24
@freeExec

1) take a character, recalculate its coordinates into screen coordinates or even better in the viewport (there they are from 0 to 1);
2) and then it’s a matter of technology to recalculate the coordinates on the canvas.
3) make every frame
4) profit
https://docs.unity3d.com/ScriptReference/Camera.Wo...

N
Nekit Medvedev, 2022-01-25
@NIKROTOS

You are an interface element located on a sheet in front of the camera, you want to position it relative to objects in the game, maybe you should make an HPBar sprite over the object you need (the sprite must be a child) and not suffer with this location?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question