- Home /
Transform.TransformPoint VS TransformDirection
Transform.TransformPoint () and Transform.TransformDirection () What is the difference? I thought for a long time or did not think they have any difference?Both return an Vector3?
Answer by Peter G · Aug 08, 2011 at 12:40 PM
That's the tricky part of Vector3's, they can store any 3 component value. So in this case, the values returned mean different things but you could interchange them if you wanted.
Transform.TransformDirection() converts a direction from local space to world space. So if you input Vector3.forward, it will return the forward vector of the character in world space. This essentially rotates the vector by the characters rotation.
Example: let's say that I want the character to move forward when I push the up arrow, but I want him to move forward on his local axis. I have to convert the local axis to world space so that it is compatible with the movement script:
var forward = transform.TransformDirection(Vector3.forward);
var input = Input.GetAxis("Vertical");
transform.position += forward * input * Time.deltaTime;
transform.Rotate( 0 , Input.GetAxis("Horizontal") * 45 * Time.deltaTime , 0 );
Transform.TransformPoint() converts a point from local space to world space. This takes the point, rotates it by the characters rotation, multiplies it by the characters scale, and then adds in the transform's position.
Example: Let's say that you want to keep the ball 2 units ahead of and 2 units to the right of the player. You use this function to set the ball at that spot.
var relativePos = new Vector3(2 , 0 , 2 );
var exactPos = playerTransform.TransformPoint(relativePos);
ballTransform.position = exactPos;
Peter, thanks for such great examples. You explained not only what it does technically, but you also gave some great examples of how/why someone would use this. UT needs you on their documentation $$anonymous$$m :)
Terrific explanation! This should seriously be part of the Unity documentation.
Answer by Elecman · Aug 08, 2013 at 07:10 AM
Just resurrecting a 2 year old thread to make another point (no pun intended).
TransformDirection() and TransformPoint() can also be used to transform points between coordinate systems.
Quick refresher about coordinate systems: A point has 3 variables: x, y, and z. These are distances, but distances relative to what? If a point is in object space, it means that the x,y, and z distances are measured realative to the object's own origin point. If a point is in world space, it means that the x,y, and z distances are measured relative to the game world origin point.
Think of a vector as an arrow with the base at the world/object origin and the head of the arrow at the point location.
Note: object space and local space are the same thing.
In the picture below the blue point location in object space is (2, 0.5) while the point in world space is (5, 5).
//Code is in C#
//TransformPoint() transforms a position from object space to world space.
//Both the position and the scale of your game object are taken into account
Vector3 pointObjectSpace = new Vector3(2f, 0.5f, 0f);
Vector3 pointWorldSpace = gameObject.transform.TransformPoint(pointObjectSpace);
//pointWorldSpace is now (5, 5, 0)
//InverseTransformPoint() transforms a position from world space to object space.
//Both the position and the scale of your game object are taken into account
pointWorldSpace = new Vector3(5f, 5f, 0f);
pointObjectSpace = gameObject.transform.InverseTransformPoint(pointWorldSpace);
//pointObjectSpace is now (2, 0.5, 0)
In the picture above the purple vector in object space has value (2, 0.5) while the vector in world space has value (0.8, 1.9). Two vectors are shown for clarity but they are identical as the position is not taken into account.
//TransformDirection() transforms a vector from object space to world space.
//Think of it as moving a vector from your object to the game world origin.
//The position and scale of your game object are not taken into account.
Vector3 vectorObjectSpace = new Vector3(2f, 0.5f, 0f);
Vector3 vectorWorldSpace = gameObject.transform.TransformDirection(vectorObjectSpace);
//vectorWorldSpace is now (0.8, 1.9, 0)
//InverseTransformDirection() transforms a vector from world space to object space.
//Think of it as moving a vector from the game world origin to your object.
//The position and scale of your game object are not taken into account.
vectorWorldSpace = new Vector3(0.8f, 1.9f, 0f);
vectorObjectSpace = gameObject.transform.InverseTransformDirection(vectorWorldSpace);
//vectorObjectSpace is now (2f, 0.5f, 0f)
//Both TransformPoint() and InverseTransformPoint() are affected by scale. Using these
//functions can have unexpected results when your object does not have a uniform scale of 1.
//If you do not want your point transformations to be affected by scale, use TransformDirection()
//and InverseTransformDirection() instead. These functions do not take the position of your
//object into account, so you will have to add one line of code:
//TransformDirection() is used here to transform a position from object space to world space.
//The scale of your game object is not taken into account.
pointObjectSpace = new Vector3(2f, 0.5f, 0f);
pointWorldSpace = gameObject.transform.TransformDirection(pointObjectSpace);
//pointWorldSpace is now (0.8, 1.9, 0)
pointWorldSpace += gameObject.transform.position; //Add this to fix the lack of position.
//pointWorldSpace is now (5, 5, 0)
//InverseTransformDirection() is used here to transform a position from world space to object space.
//The scale of your game object is not taken into account.
pointWorldSpace = new Vector3(5f, 5f, 0f);
pointWorldSpace -= gameObject.transform.position; //Add this to fix the lack of position.
//pointWorldSpace is now (0.8, 1.9, 0)
pointObjectSpace = gameObject.transform.InverseTransformDirection(pointWorldSpace);
//pointObjectSpace is now (2f, 0.5f, 0f)
Answer by Tony-Lovell · Nov 24, 2015 at 12:10 PM
Apart from the Point/Direction issue, these functions could have more explicit names to address the "which way does it convert" issue.
Better:
TransformPointLocalToWorld(), TransformPointWorldToLocal(), TransformDirectionLocalToWorld(), TransformDirectionWorldToLocal()