The use of screentoviewportpoint,worldtoviewportpoint,viewporttoworldpoint to achieve a simple drag-and-drop movement effect on three-dimensional objects

Source: Internet
Author: User

As we all know, the coordinates on the screen of our mobile phone or mobile phone are the coordinate values of a two-dimensional plane, and this coordinate is a unit of pixels, that is, depending on the device you are using, the maximum length and width of the coordinates on your phone or computer are different.

Before I did not quite understand the title of the three methods of use, took a lot of detours, and then down carefully studied a bit, the feeling is quite fruitful.

To put it simply, we are in unity in the screen coordinates is to sit down as the origin, extend to both sides, to the right of the end, assuming now our resolution is 1920x1080, then the simple, four corner corresponding to the value you can probably guess, (assuming horizontal screen) sit down (0,0), lower right (1920,0), Upper left (1080,0), upper right (1920,1085). Let this is the coordinates of our screen, in Unity mouse beyond the words, will be displayed as a negative value, this can be printed with the log statement to see, the front is the cushion, the next focus, attention ...

1. If we use the first method Camera.main.ScreenToViewportPoint (), then we can convert the point on our non-resolution screen to a value that wants the X, Y value between (0,1), Of course, it's the same as the above. If you pass in the position of the mouse that is outside the screen range corresponding to the viewport coordinates (so called) will also become negative, but do not want to divide the rate so many cases, this simple think to understand, the following is the example of the first conversion code:

Converts screen coordinates to viewport coordinates Vector3 POS1 = Camera.main.ScreenToViewportPoint (input.mouseposition);

2. Convert the position of the object in three-dimensional to the viewport coordinate position, stored in a three-dimensional vector, here the three-dimensional object coordinates converted to viewport coordinates roughly like this, if you can see this object under the game, then it wants X, y in between (0,1), If beyond our field of view is greater than a value of less than 0, this is not a problem, and then is the value of Z, because the object may be different from the camera, this value changes relatively large, I here the object in the three-dimensional scene coordinates into the viewport coordinates the main reason to get his distance from the camera, that is, Z value,

The world coordinates of three-dimensional objects are converted to viewport coordinates Vector3 Pos2=camera.main.worldtoviewportpoint (target.position);

3. Synthesize a new vector from the information group obtained in 1, 2, and then as a parameter in 4

Assign to a new variable Vector3 pos=new Vector3 (POS1.X,POS1.Y,POS2.Z);

4. Convert viewport coordinates to three-dimensional coordinates to transfer back to the position of the object, the function is accomplished

Converts a synthesized viewport coordinate to a world coordinate assignment to an object Target.position=camera.main.viewporttoworldpoint (POS);

Next is a demo of a GIF:

Please correct me if there is any unreasonable place.

The use of screentoviewportpoint,worldtoviewportpoint,viewporttoworldpoint to achieve a simple drag-and-drop movement effect on three-dimensional objects

Related Article

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.