- Home /
Difference Between Transform.TransformVector and Transform.TransformPoint
My confusion arose in the following scenario:
transform a;
transform b;
Vector3 newPosition = NewLocalPosition(a);
b.position = a.TransformPoint(newPosition);
void NewLocalPosition(transform){
based on some input ;
return new local position of transform ;
}
Transform a was descendant of many parents and transform b had no parents.
I was attempting to set the global position of transform b to the global position equivalent of the calculated new local position of transform a. I had only used TransformPoint in the past and this was giving me unwanted results. After a few hours of fiddling around i realized that TransformVector gave me the results i was looking for. I dont really understand why one works and not the other.
I would really appreciate some clarity in what the difference is as I am unable to find a clear explanation anywhere. Thankyou.
Answer by zach-r-d · Aug 05, 2015 at 11:05 AM
There are three similar methods on transform: TransformPoint, TransformDirection, and TransformVector. The difference comes down to which aspects of the transform are used or not used when performing the transform:
TransformPoint: position, rotation, and scale
TransformDirection: rotation only
TransformVector: rotation and scale only
TransformPoint is used, as the name implies to transform a point from local space to global space. For example, if the collider of a player is offset by half their height so that the transform position is at the player's feet, to get the world space position of the collider center, it would be playerTransform.TransformPoint(collider.center) because that world-space position will change if any one of the player's position, rotation, or scale changes.
TransformDirection is used to transform a direction. For example, if there were a friendly AI that always wanted to face the same direction as the player, it would set its rotation to Quaternion.LookDirection(playerTransform.TransformDirection(Vector3.forward)). There's actually a convenience property on transform called .forward that does exactly this, but bear with me. This rotates Vector3.forward (which is 0,0,1) to face the direction of the player's forward direction. So if the player's rotation is in the default orientation, it will just return 0,0,1. But if the player were looking straight down, it would return 0,-1,0. Since TransformDirection only cares about the rotation, note that the magnitude is preserved.
And finally, there's TransformVector, which seems to be the same as TransformDirection, but takes scale into account and will thus change the return value's magnitude accordingly (and probably the direction too if the scale is nonuniform). I'm not actually sure what this is useful for, since my use cases always fall under the first two, but clearly you've found a use for it!
Great description of point and direction but those are already clear to me. From what my results and the little bit on the internet is telling me that TransformVector is closer to TransformPoint than TransformDirection. Has nobody ever used TransformVector?
Sure, it's equally valid to think of TransformVector as TransformPoint ignoring position as TransformDirection but scale-aware.
I can't find any discussion of TransformVector usage online. It could be that when TransformVector would be useful, people tend to use TransformPoint or TransformDirection and then subtract or add transform.position manually.
Thank you for the summary of Point vs Direction vs Vector. This was very helpful and allowed me to finally figure out what was going wrong with a raycast.
For people finding this later and still don't see a use for TransformVector after reading this, it is used for transfor$$anonymous$$g things like velocities, accelerations, momentum. In other words, vector quantities. The main reason there are different methods for TransformPoint and TransformVector is so it can decide what to do when multiplying a Vector3 by a $$anonymous$$atrix4x4. For TransformPoint the conversion from Vector3 to Vector4 is (x,y,z,1) and TransformVector it is (x,y,z,0). For TransformDirection you conjugate (x,y,z,0) by the rotation quaternion (I'm not sure that this is exactly what Unity does, but conceptually that is what is happening).
Your answer
Follow this Question
Related Questions
Moving a Platform 1 Answer
Why using = isn´t fast enough for changing Vector3 values from the transform? 2 Answers
Player rotation = Camera Rotation 0 Answers
nullifying targetObject after a Smooth LookAt Transition 2 Answers
How would I rotate an object relative to another without parenting? 2 Answers