- Home /
TransformPoint and InverseTransformPoint
In the code below, what values are the first two variables being set to, and why?
This code is attached to the main character in the Unity 2D side scroller tutorial. For the purposes of this question assume the following Vector3 values:
Active Platform: (2,1,0)
Player: (1,2,0) The player is standing just on the edge of the platform, therefore his x coordinate is slightly off.
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
activeGlobalPlatformPoint = transform.position;
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
transform.position = transform.position + moveDistance;
Answer by Loius · Oct 04, 2010 at 03:50 PM
We start with
transform.position = (1,2,0)
activePlatform.position = (2,1,0)
we then know that
activeLocalPlatformPoint = (-1,1,0)
that is, the transform's center relative to the platform's center (TransformPoint) and
activeGlobalPlatformPoint = (1,2,0)
that is, the global position of platform.position + aLPP
We'll now move the platform up one unit -
activePlatform.position = (2,2,0)
Then, going through the function:
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
// nGPP = aP.TP(aLPP) : nGPP = (2,2,0).TP( (-1,1,0) ) : nGPP = (1,3,0)
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
// inverseTransformPoint converts world coordinates to local
// aLPP = aP.ITP( t.p ) : aLPP = aP.ITP( (2,2,0) ) : aLPP = (-1,0,0)
activeGlobalPlatformPoint = transform.position;
// aGPP = (1,2,0)
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
// mD = ( (1,3,0) - (1,2,0) ) = (0,1,0)
transform.position = transform.position + moveDistance;
TransformPoint moves a point from world coordinates to an object's local coordinates (including rotation); InverseTransformPoint moves a point from local coordinates (including rotation) to world.
Wait...theres a problem. You list activLPP as equaling (-1,0,0) but in the newGPP you list it as (-1,1,0). Where does the extra y coordinate come from? Without out it the player would not move relative to the platform.
newGPP = (something based on activeLPP); the next line is (activeLPP = something based on position). It is -1,1,0 in line 1, but then the second line overwrites it.
Answer by MaDDoX · Sep 29, 2012 at 08:45 PM
Actually it's the opposite to what Loius said. The default values, as shown in the inspector, are local. Thus "transforming" mean changing to world and "inverse transforming" changing back to the default, local coordinates. From the docs:
Transform.TransformPoint: Transforms the position x, y, z from local space to world space.
Transform.InverseTransformPoint: Transforms the position x, y, z from world space to local space. The opposite of Transform.TransformPoint.