Is there any way to position a perspective camera to move objects to specific screen points?

I have been struggling over this for a while. I need to split one perspective camera into two perspective cameras whose rectangles split down the center of the screen, but i want it to start with the players each window focuses on being in the same general position they already were on screen at first.

I have tried all sorts of things, including calculating the difference between the camera position and the player in units and pixels to determine the pixel/unit relationship and panning by the change after splitting. No dice.

If there was some way to move the camera not to center an object, but to off-center it by a specific amount of pixels, that would be an ideal fix.

You can move the camera to place a specific object at screen pixel position as long as you don’t want to restrict how the camera moves.

Construct a mathematical plane running through the object using the transform.forward of the camera. Create a ray from the pixel position, and Raycast() against the mathematical plane. Subtract the object position from hit point to generate the offset to move the camera. Below is a script to demonstrate:

  • Start with an empty scene
  • Put the camera at some arbitrary position and rotation
  • Place an object some arbitrary position within view of the camera
  • Attach the script below to the camera
  • Drag and drop the object in view into the ‘obj’ variable in the script
  • Run and start clicking

#pragma strict

public var obj : Transform;

function Update() {
	if (Input.GetMouseButtonDown(0)) {
		var ray = camera.ScreenPointToRay(Input.mousePosition);
		var plane = new Plane(camera.transform.forward, obj.position);
		var dist : float;
		plane.Raycast(ray, dist);
		var point = ray.GetPoint(dist);
		var offset = obj.position - point;
		camera.transform.position += offset;
	}
}