Precautions for coordinate transformation of Transform IN unity3D game development, unity3dtransform
Transform is one of the core classes of unity. It indicates the translation, rotation, and scaling of objects. Position and localPosition, Transform is the world space and parent space respectively. Note that the parent space is not its own space. After you have noticed this, it will be much easier to change the space.
If you want to understand the transformation process of transform. position, you can test it as follows:
Java code
- // The parent space is transferred to the world.
- Debug. Log (transform. position );
- Debug. Log (transform. parent. TransformPoint (transform. localPosition ));
- Debug. Log (transform. parent. localToWorldMatrix. MultiplyPoint (transform. localPosition ));
Copy code
Java code
- // Convert world to parent space
- Debug. Log (transform. localPosition );
- Debug. Log (transform. parent. InverseTransformPoint (transform. position ));
- Debug. Log (transform. parent. worldToLocalMatrix. MultiplyPoint (transform. position ));
Copy code
However, if transform is directly used for transformation, the result will be incorrect] Java code
- Debug. Log (transform. position );
- Debug. Log (transform. TransformPoint (transform. localPosition ));
- Debug. Log (transform. localToWorldMatrix. MultiplyPoint (transform. localPosition ));
Copy code
In this case, you should note that transform. localXXX represents the description in the parent space. XXXPosition, EulerAngles, Scale)
If you want to convert the transformation information of one space into another space, you can do this: 1. The simplest way Java code
- Transform. position = target. position;
- Transform. rotation = target. rotation;
- Transform. localScale = target. lossyScale;
Copy code
Of course, it depends on the actual situation] |