1
\$\begingroup\$

Say i have the following wheel:

enter image description here

Now i wish to use the mouse to select weapon by movement:

So

If mouse is to the left it picks the Sword if mouse is at the top it selects the bow and arrow if the mouse is in the bottom then it selects the hand

Since my game is not visually showing the mouse it has to be at any position. i

However, I am not quite sure how to do this? So I am hoping some of you might be able to help. I have looked at getting the mouse position as a vector 2 but I am unsure on how to get the correct position.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ In what way is the mouse position that you get "as a vector 2" not the correct position? How does it differ from the value you'd deem correct? \$\endgroup\$ – DMGregory Jul 22 '19 at 22:39
  • \$\begingroup\$ I think breaking it down in steps will help. First you display the UI Wheel on mousedown then you get and store the Input.mousePosition at that point next you would check against the first position whether you are draggin up,down left or right to show the hovering effect of choice and then on mouseUp you would select the one you were on. \$\endgroup\$ – Justin Markwell Jul 23 '19 at 0:14
0
\$\begingroup\$

Here's my example: attach this script to the arrow button, fist button etc.

public class example : monobehavior {

    void OnMouseOver()
    {
       //select weapon
    }
}

For your problem of the mouse not being displayed, you could add a sprite to your canvas that is basically the boundary of the select area, and add the script to that. Then you could disable the sprite renderer so it doesn't show up in the game.

If you only wanted the weapon selected if the player had the mouse over it and released the key that brought the menu up, you could put your code in update (you may need to do this already) and add an if statement:

if(Input.GetKeyUp(your key choice))

to the OnMouseOver() function

\$\endgroup\$
0
\$\begingroup\$

You can do this mathematically:

Vector2 menuPosition = radialMenu.transform.position;  // Assumes the center of your menu
Vector2 mousePosition = Input.mousePosition;
float mouseAngle = Mathf.Atan2(mousePosition.y - menuPosition.y, Input.mousePosition.x - menuPosition.x) * Mathf.Rad2Deg;

This should get you the angle (in degrees) of the mouse position from the horizontal. Thus, you would know that any value between 0 and 120 would correspond to one icon, any value between 120 and 240 would be the next, and so on. In your case, you will need to add an offset to the calculation to make sure the start value lines up with the edge of your first icon.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Keep in mind that transform.position gives you a position in world space, and Input.mousePosition gives you a position in screen space. These are not directly comparable without some additional work to get them into the same coordinate space. \$\endgroup\$ – DMGregory Apr 20 at 18:32
  • \$\begingroup\$ Huh; I pulled this code directly out of my game. You're right though. \$\endgroup\$ – Matt Apr 20 at 18:38
  • 1
    \$\begingroup\$ If your radial menu is in a UI canvas whose dimensions closely match the pixel dimensions of your screen, then you may get a good match between world space and screen space here. But it's still safer to go through the coordinate conversions so you're not counting on this coincidence to always hold (say your UI designer updates the canvas scaling settings and now the weapon wheel suddenly stops working correctly...) \$\endgroup\$ – DMGregory Apr 20 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.