Converting orthographic position to perspective?

I’ve got a perspective camera with an orthographic camera overlayed which contains dice.

I’m trying to dynamically animate dice in the perspective camera and have them fly to the positions of their orthographic counterparts. When I do this, the animated dice on the perspective layer end up in positions that don’t line up (outlined in green in my attached image). I assume this is due to the difference in perspective.

Is there a way to find the positions that visually line up with the orthographic camera’s positions?

They will never line up perfectly because with perspective projection you can see the sides of the cubes if they are not in the center.

For an approximate solution look into Camera.ScreenToWorldPoint .

1 Like

Thanks for the help. I know they’ll never line up perfectly, I just need them to share the same center position so I can make a swap.

I’ve tried some combination of Camera.ScreenToWorldPoint but I’m missing something in my understanding. Would I use the perspective camera or the orthographic camera to calculate this position? Where am I getting the position value that should feed this function? The Rects on the orthographic view dice?

You would use the perspective camera and you’d feed it the 2D center of the orthographic view dice rectangles along with a world space distance from the camera as the z coordinate (value between perspectiveCamera.nearPlane and perspectiveCamera.farPlane). The smaller the value, the closer/bigger the 3d cube will be.

This will basically calculate a world position along the ray that originates from your perspective camera through the given pixel.

1 Like

Thanks a lot cod3 m0nk3y!

I ended up with this and it worked (I’ve got an arbitrary z position in there that works fine):

Vector2 dieCenter2D = CameraManager.instance.orthographicCamera.WorldToScreenPoint(MainGameManager.instance.playerSlots.slotControllers[i].GetComponent<RectTransform>().position);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(dieCenter2D.x, dieCenter2D.y, 10));
1 Like