Unity Coordinate Transformation

Source: Internet
Author: User

The most common coordinates of unity are screen coordinates, world coordinates, and ngui coordinates, which are relative to the coordinates of the parent object ).

But sometimes it is difficult for these coordinates to convert each other, so that you can grasp the head. You have to create a virtual body and change its parent node (parent) Back and forth ),

However, Unity provides two very useful methods. Solved all coordinate conversion problems,

Inversetransformpoint: Convert absolute coordinates to relative coordinates, that is, the relative coordinates of the world coordinates to be placed under the parent node,

Transformpoint: Convert relative coordinates to absolute coordinates, that is, convert the relative coordinates under the parent node you want to put into world coordinates,

The calling method is as follows. For example, you want to convert the cursor position to the ngui coordinates under a parent node.

The operations are as follows:

Vector3 VEC = new vector3 (); Ray = uicamera. maincamera. screenpointtoray (input. mouseposition); // raycasthit hit; If (physics. raycast (Ray, out hit) {VEC = the parent node object you want to transfer. transform. inversetransformpoint (hit. point );}

In this way, the relative coordinates of the parent node of ngui are obtained. Other objects are also used in this way,

This ensures the smoothness of Operation coordinates, and does not need to create so many virtual bodies,

Of course, the usage of converting relative coordinates to absolute coordinates is the same. You do not need to create a virtual body, you want to convert a location under a parent node to a global coordinate,

Your current parent object. Transform. transformpoint (relative to the location of the parent object );

In this way, it can be converted to the world coordinates. The world coordinates are used for all objects in a general scenario. You can do anything you want.

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.