ScreenToWorldPoint without changing Z position or adjust scaling to compensate?

Hi,

I have an app that has a 2d target/site that follows a 3d object in space. I have the 2d site following the 3d object perfectly, but the 2d site changes its z position and so the 2d site scales.

The challenge of this project is that it’s an augmented reality app, I’m using ARToolKit. So I have to get the screen position from the ARToolKit camera (which moves), then using that screen position translate them to the world position in my UI Camera. Again this is working, but the 2d object scales as it moves along the z axis, of course. But I don’t want it to scale.

Any ideas how I can stop this? Either not allow movement along the z axis but somehow still center the 2d object on the 3d object.

Or should I somehow compensate the scaling of the 2d object by how much the 2d object moves on the z axis.

Below is my code:

// targetCamera is the ARToolKit camera, and targetObject is the 3d object ARToolKit displays over the image marker
Vector3 targetScreenPos = targetCamera.WorldToScreenPoint(targetObject.transform.position);

// orbitalCamera is the camera for the 2d object. the script is attached to the 2d object, so the ‘transform’ belongs to it.

Vector3 oribitalWorldPos = obitalCamera.ScreenToWorldPoint(targetScreenPos);
transform.position = oribitalWorldPos;

Camera.ScreenToWorldPoint takes the x,y position on the screen and raycasts out a Z Depth to give you proper world Position that will when rendered translate back to the original x,y screen position. So the Z is perfectly arbitrary, your currently using the target’s Z position (from targetScreenPos) you can use a static Z position just fine and your object won’t scale:

Vector3 targetScreenPos = targetCamera.WorldToScreenPoint(targetObject.transform.position);

targetScreenPos.z = transform.position.z
Vector3 oribitalWorldPos = obitalCamera.ScreenToWorldPoint(targetScreenPos);
transform.position = oribitalWorldPos;

Hi takatok. Thank you for taking the time to help. I applied what you suggested but this caused the target object to move up the z axis, the movement never stopped, the z position kept decreasing well into the negative.

However I was able to fix the issue by simply changing the camera’s Project to Orthographic.

cheers.