Unity3d tutorial: Convert 3D coordinates into 2D coordinates. 3D coordinates are called World Space in unity, and 2D coordinates are called screen space, which is based on Pixel. The following uses the iPad 1024x768 () resolution as an example:
The upper left corner is (0, 0)
The bottom right corner is (1024,768)
Unity3d Tutorial: how to convert 3D coordinates into 2D coordinates
Function creation: Click the 3D cube in the screen and generate the corresponding GUI at the center of the cube.
This function is mainly used: Camera. worldtoscreenpoint ();
Add the following JavaScript code to the camera object.
Unity3d Tutorial: how to convert 3D coordinates into 2D coordinates
1. raycast click Settings: You can use the mouse (PC)/finger (IPAD, mobile devices) to perform click tests.
2. Use the "Tower" label name to separate different 3D objects.
3. Use this function to obtain the X, Y, and zcoordinates of the clicked 3D objects and then convert them to the 2D coordinates of screen space through camera. worldscreenpoint.
4. then we use the obtained 2D coordinates to generate the UI on the relative position of the device screen * Note: The Y coordinate is to subtract the obtained object y coordinate from the height of the device screen.
Result example:
Unity3d Tutorial: how to convert 3D coordinates into 2D coordinates
The square UI in the center of a vertex is generated in the upper left corner of the center vertex.
Application: Click an object on the screen in the tower defense game and then sell or upgrade it.
* Copyright statement: During reprinting, please use hyperlinks to indicate the original source and author information of the article.
* This article is from: unity3d tutorial Manual
* Link to this article: http://www.unitymanual.com/3917.html