- Home /
Why transform.TransformPoint(transform.position) prints differently than single transform.position?
From a script attached on an object, which actually casts a Linecast from itself to a "target" object, I instruct to print the following two statements:
print (target.transform.TransformPoint(transform.position));
and
print (target.transform.position);
The two print instructions give these information respectively:
(2*X, uncertain relationship*Y, 2*Z)
and
(X,Y,Z)
Both of the objects, the one with the script attached and the "target" are not parented to anything and their scale values are 1,1,1. They are also both the Cube GameObject. Isn't it supposed that both statements should print the world coordinates of the "target" object?
Do you think is possible to draw a conclusion from this data or it might be more complicated? Any guesses however? Thank you very much.
EDIT: I also tried the experiment to just have a script with only the print statements. Moreover, I asked it to print the same for the object carrying the script itself. I get the same result, only that in the second case, also the Y coordinate has a double ratio. So, I guess it has to do with the way I have structured the statements. In particular, any idea how the TransformPoint functions? Since the transform.position is rather straight forward.
Answer by robertbu · Jun 07, 2013 at 02:15 PM
TransformPoint() translates a point from local space to world space. 'transform.position' is already in world space. The local position of an object will be Vector3.zero, so if you want to compare the two, use:
print (target.transform.TransformPoint(Vector3.zero));
That's great! Only any clue what causes this double ratio, when I write it like above?
Say I had an object at (2,3,4). The TransformPoint() will take (0,0,0) and turn it into (2,3,4). That is, it translates (i.e. adds to) the point by (2,3,4). So if I feed (2,3,4) into TransformPoint(), I get (2+2, 3+3, 4+4) which is (4,6,8) which is (2*2, 2*3, 2*4) which is what you see in your print statements.
Trying to explain my own concern, with the original statement, it's like we ask to tell as the world coordinates of a point, which is located a vector(transform. position) away from the transform.position of the current object. Therefore, it's two times these coordinates in the world system. I see… Now i'll try to implement it to my more complicated case. Thanks!
Yes the clue that robertbu gives you:
TransformPoint : local space --> world space
transform.position: a worldspace position
TransformPoint(transform.position) world space (interpreted as localspace) --> garbage-space
From your "uncertain relationship" i can see that your object is only rotated around the x-axis. I could paint a diagram, but i don't have the time and this answer should already answer the question.
Try to understand what's localspace and what's worldspace.
Oh, yes that's right! Thanks, your answer helped me to clear things out!
Your answer
![](https://koobas.hobune.stream/wayback/20220613103712im_/https://answers.unity.com/themes/thub/images/avi.jpg)