I have a radial menu that I pop up when the player right-clicks. The biggest difference, I think, is that I use the Input.mousePosition
to get the screen coordinates, whereas you’d have to use WorldToScreenPoint
to get the screen position of your tower, as @AlwaysSunny mentioned.
To make it work, I created an empty that contains my handler script (the one that makes the pop-up appear, checks for the cursor moving away, and hiding the menu again).
This empty has a Canvas as a child, in Screen Space - Overlay mode, with appropriate scaling, etc. In this canvas, centered around (0,0), I created the radial menu, icons, buttons with callbacks, etc. The empty starts life as not active.
When I want to show the pop-up, I call aShowRadial
method in the hander, and pass in the 2D position that I want the menu to appear. Within the method, I do a simple gameObject.transform.position = new Vector2(x, y)
and then gameObject.SetActive(true)
. This moves it to the position I want, shows the menu, and activates the buttons. Note: I do some math to prevent the menu going too close to the screen edge and being cut off, otherwise I would just set the position to the Vector2 screen space instead of creating a new one.
To hide the menu again, I just call gameObject.SetActive(false)
within the Dismiss
method.
Thinking about it, it would probably also work well to leave the empty at its origin, and move the main menu panel inside the canvas, rather than moving the empty.