Unity3d Tutorial: how to convert 3D coordinates into 2D coordinates

Source: Internet
Author: User

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

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.