Skip to main content
Some remark
Source Link
Leggy7
  • 1.5k
  • 15
  • 32

I have a 2d game where I want to display UI element perfectly aligned to specific gameObjects.

I have an ortographic camera and I used to Camera.main.WorldToScreenPoint() function giving it the gameobject position modified, for instance, by +1 on X component to get the UI element on its perfect right.

My problem is that the element is not on the gameObject's perfect right, but sensibly above. See this schema:

Expectation:

G -> UI element

Reality:

---> UI element
G

I tried also to use the WorldToViewportPoint() but it's not giving the desired result.
So, I think this is the right way to go but I can't guess what I'm missing.
Any clue?

EDIT:
After a little bit more observations it seemed that the UI element alignment depends somehow on it's distance from the center of the screen (of the camera viewport I guess), so that if the gameObject is below half height the UI element y position won't be on the perfect right but slightly below it and if above half heigth will be slightly above the perfect right. Same for x coordinate. Is there a way I could control this?

I have a 2d game where I want to display UI element perfectly aligned to specific gameObjects.

I have an ortographic camera and I used to Camera.main.WorldToScreenPoint() function giving it the gameobject position modified, for instance, by +1 on X component to get the UI element on its perfect right.

My problem is that the element is not on the gameObject's perfect right, but sensibly above. See this schema:

Expectation:

G -> UI element

Reality:

---> UI element
G

I tried also to use the WorldToViewportPoint() but it's not giving the desired result.
So, I think this is the right way to go but I can't guess what I'm missing.
Any clue?

I have a 2d game where I want to display UI element perfectly aligned to specific gameObjects.

I have an ortographic camera and I used to Camera.main.WorldToScreenPoint() function giving it the gameobject position modified, for instance, by +1 on X component to get the UI element on its perfect right.

My problem is that the element is not on the gameObject's perfect right, but sensibly above. See this schema:

Expectation:

G -> UI element

Reality:

---> UI element
G

I tried also to use the WorldToViewportPoint() but it's not giving the desired result.
So, I think this is the right way to go but I can't guess what I'm missing.
Any clue?

EDIT:
After a little bit more observations it seemed that the UI element alignment depends somehow on it's distance from the center of the screen (of the camera viewport I guess), so that if the gameObject is below half height the UI element y position won't be on the perfect right but slightly below it and if above half heigth will be slightly above the perfect right. Same for x coordinate. Is there a way I could control this?

Source Link
Leggy7
  • 1.5k
  • 15
  • 32

Align UI element to gameObject in 2D

I have a 2d game where I want to display UI element perfectly aligned to specific gameObjects.

I have an ortographic camera and I used to Camera.main.WorldToScreenPoint() function giving it the gameobject position modified, for instance, by +1 on X component to get the UI element on its perfect right.

My problem is that the element is not on the gameObject's perfect right, but sensibly above. See this schema:

Expectation:

G -> UI element

Reality:

---> UI element
G

I tried also to use the WorldToViewportPoint() but it's not giving the desired result.
So, I think this is the right way to go but I can't guess what I'm missing.
Any clue?