The meaning and usage scenarios of TransformPoint

Significance: the relative coordinates of any point that is a certain distance away from the object (or sub-objects are rarely used here, the factor object can directly use transform. convert to world coordinates

Vector3 WorldPosition = gameObject.transform.TransformPoint(xdistance, ydistance, zdistance);

The obtained WorldPosition is the world coordinates of the point (the child whose localPosition is (xdistance, ydistance, zdistance)) with distances of (xdistance, ydistance, zdistance) in three directions relative to the object (parent object). It takes into account the rotation and scaling of the parent object, regardless of whether the parent object has rotation and scaling, feel free to use it, and you can get the correct world coordinates in one sentence.

 The code mounts the parent object on the cube:

Vector3 WorldPosition = gameObject.transform.TransformPoint(1, 1, 2);
        Debug.Log($"The world coordinate is {WorldPosition}");

Test instance 1 has no rotation and no scaling, and the coordinates are as follows:

 

Test instance 2 parent object cube only rotates

 Test instance 3 parent object cube only scales

 

 Test instance 4 when the parent object cube has rotation and scaling

 

 It can be seen from the above that in all cases, TransformPoint can be directly used to obtain the world coordinates of any point at a certain distance relative to the object

Guess you like

Origin blog.csdn.net/weixin_44345862/article/details/127389507