I’m working on an inventory system for my game. Among such things, I had a UI Tooltip element that would follow the cursor and raycast to find out what the cursor was hovering over, and find out if it could display the gameObject’s (custom) tooltip component. It worked as easily as
TooltipUI.position = cursorPos;
Instead of using sprites to display all my items, I wanted to use 3D models rendered in the respective item slots. I previously had it set to sprites, so I disabled all those parts of my code where it would update the slots with the images from the sprites the items had referenced. Looking how to do the 3D overlay shenanigans, I learned about Camera Stacking, and thus I now have a main camera and a UI camera. I have their projection matrices tied together so I don’t have to bother managing them separately (at least a demo project showed that, so I just followed suit). So I changed the canvas settings on my “PlayerUI” gameObject, and thats where the issues started. Screen Space - Overlay swapped to Screen Space - Camera, I targeted the Render Camera to the new camera I’m using to manage the UI, gave the cameras the correct layers to render, and the code for moving the tooltip around broke. After messing with it extensively for a few hours, I got this result.
As shown, I’ve managed to edit and change the code so that it follows the cursor…but it has some weird scaling action going with it and will not simply go 1:1 with the cursor position.
PlayerInput myInput = Player.GetComponent<PlayerInput>();
Vector2 cursorPos = myInput.actions["UI/Point"].ReadValue<Vector2>();
var screenPoint = new Vector3(cursorPos.x,cursorPos.y,100.0f);
TooltipUI.position = Camera.main.ScreenToWorldPoint(screenPoint);
Far as I can tell, this is where the problem rises up from. I’m using the Unity Input module for my input, and its been working pretty alright so far. cursorPos does in fact give me the correct cursorPos, at least before the swap in canvas render mode, so I’m pretty sure thats not the issue. I’ve changed “ScreenToWorldPoint” around from ScreenToViewport, ViewportToWorld, etc, and while I haven’t specifically checked the trial/error list, I figured I’d come here for advice and try and understand it further rather than try my luck on another 100 iterations of code.
Not sure if its related, but additionally whenever I try and directly change the position of the Rect Transform for the TooltipUI component, half the time it doesn’t actually modify the position of the component.
How can I get it so that it once more will follow my cursor correctly? And furthermore, if anyone has any help/tip/advice for rendering 3D objects in the overlay, I’d appreciate it and any tutorial links that may be available.