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.