Differences Between TransformVector,TransformPoint and TransformDirection
In this article, I am going to explain the differences between TransformVector, TransformPoint and TransformDirection that I believe there are some misunderstanding and missing information in Unity Scripting API documentation about these transform methods. Before comparing these methods, I strongly recommend you to cover the logic of local space and world space notions. We can simply explain local space notion as it considers rotation and direction of relative to the game object unlike world space which is relative to the game object in game world. Let’s move on to our main topic without distracting too much and start with TransformPoint.
TransformPoint transforms position from local space to world space.It is affected by position, rotation and scale of relative game object that you call. For instance, I have an object at (1,1,1) vector and the TransformPoint takes (2,0,4) vector then I get the return point as (1+2, 1+0, 1+4) which is (3,1,5) vector.
var initialPosition = new Vector(1,1,1);
var parameterPosition = new Vector(2,0,4);
var targetPosition = transform.TransformPoint(parameterPosition);
As I said above, TransformPoint is also affected by scale.Let’s implement the same code by increasing the scale by 2 and check the returned vector which is targetPosition.
var scaleFactor = new Vector3(2,2,2);
transform.localScale = scaleFactor;
So, according to the returned value we can simply see that, the logic of scale effect can be formulated as (parameterPosition*scaleFactor) + initialPosition.
TransformDirection is used to transform a direction from local space to world space. However, there is a point that we should consider about is that TransformDirection is not affected by position and scale. It is only affected by rotation and magnitude is preserved.Let’s remind our simple code above.The biggest differences between TransformPoint and TransformDirection is to the returned vector has the same length as direction. So, when we implement the same code block above by chancing the method as TransformDirection, regardless of our initial position, we get the vector that we gave as a TransformDirection parameter which is (2,0,4) vector.
targetPosition = transform.TransformDirection(parameterPosition);
When we apply the same scale factor to our transform, we observe that our targetPosition is not changed which means that TransformDirection is not affected by scale.
Lastly, we are going to understand TransformVector logic. TransformVector is not affected by position such as TransformDirection, but it is affected by a scale.Let’s prove it. We are going to use same scale factor function above and check the result value.
var targetPosition = transform.TransformVector(parameterPosition);
Simply, we also can formulate it as parameterPosition*scaleFactor.
In conclusion, these are three similar transform methods which are affected and not affected by position, rotation and scale. To sum up even more we can create a small table and compare these methods.