Talking about the relationship between Unity3D world coordinates and local coordinates, and the conversion between them

             Talking about the relationship between Unity3D world coordinates and local coordinates, and the conversion between them


table of Contents

1. Introduction to the blog post

2. Content

(1) Relativity

(2) World coordinates

(3) Local coordinates

(4) Convert local coordinates to world coordinates

(5) Convert world coordinates to local coordinates

3. Push

4. Conclusion


1. Introduction to the blog post

Talking about the world coordinates and local coordinates in Unity3D, and the mutual conversion between the two.


2. Content

(1) Relativity

First of all, the most important thing we have to understand is the relativity of position. Only by comparing with each other can we judge the position.

(2) World coordinates

The world coordinate is the only position corresponding to the object at the highest level with reference to the world coordinate system in Unity3D. For the object at the highest level, the position in the Inspector panel is the world coordinate. There is a position property in the Transform API, which can directly obtain the world coordinates of the object. No matter in any level, the obtained object is the object's position. The only world coordinate.

(3) Local coordinates

The position information obtained by taking any object as the reference system instead of the world zero point as the reference system is the local coordinate, for example: object A, its sub-object B, and the position information of B in the Inspector panel is B to A is the local coordinate obtained by the zero point, so we can get a message, if B is no longer a child of A, but at the same level as A, then the coordinate of B should be equal to the previous local coordinate + the coordinate of A, if A is an object in world coordinates, so the result is B's world coordinates.

(4) Convert local coordinates to world coordinates

It can be seen from (1) that the world coordinates of the object can be obtained directly through transform.position

It can be seen from (3) that if the parent B of object A is an object in the world coordinate system, the world coordinates of object A = local coordinates of object A + world coordinates of B.

The Transform API provides a method TransformPoint, which can obtain the position of the object's world coordinates relative to any point, so the value of vect below is the cube's world coordinates.

var vect = cube.TransformPoint(new Vector3(0,0,0));

(5) Convert world coordinates to local coordinates

The Transform API provides a method InverseTransformPoint, which can obtain the local coordinates of any object relative to another object. In the following code, the value vect is the local coordinates if the cube is a child of fat. 

public Transform fat;

public Transform cube;

var vect = fat.InverseTransformPoint(cube.transform.position);

3. Push

博主Github:https://github.com/KingSun5


4. Conclusion

        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

 

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/98123268