- Home /
ScreenToWorldPoint on orthographic cameras has different results in play and edit mode
Hello Unity Answers, My work progress is currently hindered hindered by the fact that the ScreenToWorldPoint method has different results in play and edit mode when used with an orthographic camera.
My Test behaviour: (The MethodButtonAttribute allows me to invoke a method from the inspector)
using SevenBiT.Inspector.Attributes;
using UnityEditor;
using UnityEngine;
using System.Collections;
[MethodButton("TestPoints", "TestPoints")]
public class PointTestBehaviour : MonoBehaviour
{
public Camera PerspCam;
public Camera OrthoCam;
public void TestPoints()
{
float width = Screen.width;
float height = Screen.height;
Debug.Log(
"Persp: " + PerspCam.ScreenToWorldPoint(new Vector3(0, 0)) +
"\nOrtho: " + OrthoCam.ScreenToWorldPoint(new Vector3(0, 0)));
Debug.Log(
"Persp: " + PerspCam.ScreenToWorldPoint(new Vector3(width, 0)) +
"\nOrtho: " + OrthoCam.ScreenToWorldPoint(new Vector3(width, 0)));
Debug.Log(
"Persp: " + PerspCam.ScreenToWorldPoint(new Vector3(width, height)) +
"\nOrtho: " + OrthoCam.ScreenToWorldPoint(new Vector3(width, height)));
Debug.Log(
"Persp: " + PerspCam.ScreenToWorldPoint(new Vector3(0, height)) +
"\nOrtho: " + OrthoCam.ScreenToWorldPoint(new Vector3(0, height)));
}
}
This has the following results: (Top: Edit mode, Bottom: Play mode) http://prntscr.com/3sspan
In case a video can demonstrate the problem better: https://www.youtube.com/watch?v=J40oOd-mjC8
I would assume that this is related to the different mathematical formulas required to calculate the world points which haven't been implemented in the edit mode (or the play mode?). Or it could be me missing important information, or there is a known workaround for orthographic cameras which I haven't been able to find.
Best regards, psycho
Answer by Wisteso · Jun 14, 2014 at 09:12 PM
I don't think you can rely on consistent results between Play and Edit mode when using ScreenToWorldPoint. I think this is because the camera dimensions depend upon the viewport dimensions, and those dimensions change between Play and Edit mode. This is because the camera dimensions are normalized. If you want non-normalized dimensions, I think you need to use the "pixelRect" property via code (it's not exposed in the inspector).
You can probably test the Play/Edit resolution difference by printing the results of Screen.width/height in Edit mode and in Play mode.
Thank you. This already helped out a lot. But now I'm facing another problem: A transform should have a specific offset from the top left corner of my camera. The problem is that it that the offset gets completely screwed up if the game view gets resized on the y-axis.
public Camera OrthoCam;
public Transform TestTrans;
public Vector2 Offset;
private void Update()
{
Rect r = OrthoCam.pixelRect;
//float width = r.width;
float height = r.height;
TestTrans.position = OrthoCam.ScreenToWorldPoint(new Vector3(0 + Offset.x, height + Offset.y, 10));
}
Again, in case some visualization is required: https://www.youtube.com/watch?v=46a3jVm4boI
Its probably related to the the aspect ratio of the camera changing. But the weird thing is that everything says fine if the game view gets resized on the x-axis.