- Home /
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.
Answer by robertbu · Aug 24, 2014 at 10:48 PM
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; } }
Your answer
Follow this Question
Related Questions
Blur edges of an overlapping camera 1 Answer
Anti-Aliasing causes post processing effects to flip. 3 Answers
What is a reasonable number of draw calls relative to one hardware configuration ? 6 Answers
Unity is not using my GPU properly 1 Answer
How to create an object from 3D points in a text file?! 2 Answers