- Home /
ScreenToWorldPoint for different ortographic size
I need to calculate world point from screen coordinates for orthographic camera. Normally I would use Camera.ScreenToWorldPoint method and be happy with it, but I need to calculate this point for camera with ortographic size different from current one. How would I go about this?
Here is exactly what my problem is in case I'm overthinking and there is a simpler solution. 1. I have a point in world space. 2. I have a square on UI with it's upper-left corner "connected" to world space point 3. I want to move my camera so center of that point is in the middle. Easy one, I calculate world position for that center and I move my camera there. 4. I want to change the camera to some orthographic size (that I know beforehand) while it's moving. 5. Problem: when I'm changing the size there is some offset created and world position I calculated beforehand is no longer valid
Answer by Casiell · Jul 15, 2019 at 01:09 PM
As per usual, right after I posted the question I suddenly got like 5 ideas how to solve it. I will leave the answer here in case someone comes looking for it.
Basically I divided my point into two parts: 1. World space position of the corner (which I already had) 2. Offset from the corner to center in screen space units
Next step was to convert my offset from screen space to world space units. As I have an orthographic camera it was pretty easy. Basically the code boiled down to this:
float cameraVertical = targetOrthographicSize * 2;
float cameraHorizontal = cameraVertical * camera.aspect;
offset = new Vector2(offset.x * (cameraHorizontal / Screen.width), offset.y * (cameraVertical / Screen.height));
MoveCamera(worldPosition + offset);
Your answer
Follow this Question
Related Questions
Forward and back movements with a camera emulating an isometric view 1 Answer
Buttons move with the camera and I don't want that 0 Answers
Help with Texture following Camera!/How to create an effect like this in Unity 3d? 1 Answer
TPS camera in multiplayer... 2 Answers
How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers