- Home /
Confuse about transform.InverseTransformPoint
Hi anyone please help... I am confuse about the use of transform.InverseTransformPoint.
I am currently following the tutorial : TANKS! Unity Tutorial - Phase 3 of 8 - Camera Control,
when come to camera setup on zoom, here is the codes:
private float FindRequiredSize()
{
Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
float size = 0f;
for (int i = 0; i < m_Targets.Length; i++)
{
if (!m_Targets[i].gameObject.activeSelf)
continue;
Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.y));
size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.x) / m_Camera.aspect);
}
size += m_ScreenEdgeBuffer;
size = Mathf.Max(size, m_MinSize);
return size;
}
Here is my question/problem:
1st question: I don't understand about what InverseTransformPoint do to the object transform, my understanding about InverseTransformPoint from unity documentation is "transforms position from world space to local space", but isn't all object local transform is 0,0,0 ???, and I also do the test with simple script:
void Example()
{
result = transform.InverseTransformPoint(transform.position);
Debug.Log (result);
}
and the output turn out always 0,0,0 no matter where I move the object.
2nd question: In tank! tutorial script, desiredLocalPos and targetLocalPos are both transform from world position to local position, and if local position is 0,0,0 , then why this code exist? --->
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
doesn't it means "Vector3 desiredPosToTarget = 0,0,0 - 0,0,0 ???
I believe I misunderstood how InverseTransformPoint work and hopefully someone help me out. Thank you!
Answer by kubajs · Sep 16, 2016 at 02:23 PM
I guess now you probably understand the problem but somebody might be desperately searching for the answer as well so I'll try to explain a bit. You need to convert the global positions to camera rig's local space as you want to count the distance between the camera rig and your target on camera rig's local X and Y axes. Therefore you need to "transfer" the target object from global (world) position to local camera rig position. See the picture visualized in the tanks tutorial in 20:37 and then compare it with the camera rig gizmo in local space in your unity scene. Local camera Z axis is always the center point where the camera rig is pointed. Now let's focus to our target only (tank). In case tank directly intersects local Y or Z axis of the camera rig, then the X axis is 0 in local position. In case tank intersects X axis of the camera rig (in local position), then the Y and Z axis is 0 in local position. Basically visualize the tank's position in 2D instead of 3D in your head. Let say camera rig's Y axis goes through tank in 2D but in 3D it's completely outside. And that's exactly what your're looking for in the local space. Nice solution to understand the logic is to visualize it directly in unity and you will definitely understand. 1. select the camera rig (not the camera!) 2. switch to local space in scene view 3. see the Z axis is in parallel with the camera rig (and points directly to the center of camera frustrum and center of far camera plane. 4. Now see the direction of camera rig's Y and X axes. 5. Add Debug.Log method with your tank transform object converted to local position as argument of the method. E.g. Debug.Log(transform.InverseTransformPoint(m_Targets[0].position)); in the for loop of FindRequiredSize method. 6. run the game and try to intersect camera rig's Y local axis. with the tank (doesn't matter in what distance from camera). 7. When you intersect the Y local axis of the camera rig, notice Debug.Log displays (0, , ). Tank is kind of "projected" on camera rig's local Y axis now, despite in it's completely outside in world space. It means X offset is now zero. 8. When you intersect the X local axis of the camera rig, notice Debug.Log displays (, 0, 0). Play with the tank's position a lot and watch scene view and you'll find the solution fast. It doesn't matter how far in the west or east direction the tank is located (direction of X local axis), the local position Y and Z axis is always 0 in case tank intersects the camera rig's X axis and that's it.
You basically cannot count the distance in the global (world) space as in this case it doesn't make any sense. Both desiredPosition and targetPosition must be converted to the camera rig's local space. As camera's local Z axis is always in parallel with the direction camera points to, it means X and Y axis is always perpendicular to the camera's view center axis. Hope you or anybody can understand my explanation. It's difficult to express it in wolds. The simulation in unity is the best to understand the logic.
Always try to visualize the tank in your head as projected to the camera rig's gizmo and in the best in 2 dimensional world.