Create a popup menu at specific location

I Have a tower defense game and when a tower is clicked I would like a circular menu with buttons to pop up centered on the tower. I have created a panel with the buttons on it but do not know how to make it appear where the tower is44269-menu.jpg

I have looked through the “Creating UI elements from scripting” page but cant figure it out on my own. I know you can enable and disable menus and that is the most common way to make a popup menu but I need it to appear where the tower is and be moved whenever you click a different tower. I am using JS. Any help would be greatly appreciated.

For such a thing, it would make sense to have only one instance of this group-of-controls, and move it around / activate it as needed.

How you will go about positioning the object relative to a world space coordinate depends upon the game’s nature and the canvas projection settings.

If it’s a world-space canvas, you might be able to simply give the group-of-controls the tower’s world-space position coordinate. Otherwise, you’ll need to translate the world space tower position coordinates into canvas space. See:

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.