Unity Ugui gets the exact click position of the mouse on the screen

Source: Internet
Author: User

To get the exact click position of the mouse on the screen, do not write randomly, what conversion coordinate system, what Ray Ray detection is a cloud.

1, the conversion coordinate system is only relative, and can not accurately achieve the current mouse click on the screen position;

2,ray detection, hit is the need to collide, no collision, get what?? (0,0,0).

So, take a look at the following positive solutions.

When I converted with a coordinate system, I found that there was no change in the value and there was a mess online.

Actually very simple, input.mouseposition itself is the screen coordinates (two-dimensional),

cannot be used directly because the screen datum point in the center of the screen, you need to lose one-second coordinate values, that is, minus one-second screen width, height.

Place the datum point in the lower-left corner of the screen, which is (0,0) in the lower-left corner.

At this point the screen coordinates of the m_panel correspond to the x, y values of the tranpos.

Transform (recttransform) M_panel;

float X = input.mouseposition.x-screen.width/2f;
float Y = input.mouseposition.y-screen.height/2f;
Vector2 Tranpos = new Vector2 (x, y);
M_panel.localposition = Tranpos;

Note: You need to consider the M_panel anchor point, for example: So to speak, the anchor point corresponds to the coordinate center point.

Unity Ugui gets the exact click position of the mouse on the screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.