I have a problem positioning a tooltip in the right position, with Screen Space Camera.
Let’s say I have this structure:
Canvas (Screen space - camera, Canvas scaler - Scale with screen size)
+- Hotbar (Pos Y = 30, Anchor bottom, center, Scale 0.8, horizontal layout group)
| +-- Item 1
| +-- Item 2
+- Tooltip
Now when I hover an item in the hotbar, i want to show the tooltip on that exact position of the item.
I use itemSlot.transform.localPosition to get the position.
There are two problems:
-
Position X - Since the hotbar is scaled 0.8, the position is offset. I solved this by multiplying localPosition with scale of the hotbar.
-
Position Y - After scaling, this should be right, but it’s not, since the hotbar is anchored to the bottom of the screen. So I need to get the localPosition.y of the hotbar, and subtract it to get the correct position.
Is there any way to get the absolute position (or relative to the canvas they are all in), so it works no matter where the hotbar is positioned or scaled?
I also have other items, like an inventory window, that are nested within other UI elements, like a vertical layout group, and another window that opens on the right side of the screen.
It would be great to just get the position, without having to do lots of calculations that will probably break when I add or change UI elements.
Also I assume using Scale 1 on everything makes the scaling problem go away, but since we’re fiddling with the UI, setting scale is a quick way instead of adjusting the contents of each element manually.
Thank you!