Hi!
I ran into a problem that’s driving me nuts.
Here’s the test case:
Create a new 3D project in Unity and create a plane with…
Position.x = 0.5, Position.y = 0.0, Position.z = 0.5
Rotation.x = 0.0, Rotation.y = 0.0, Rotation.z = 0.0
Scale.x = 0.1, Scale.y = 1.0, Scale.z = 0.1
which leads to a square lying on the xz plane with scale 1x1.
For live debugging information I create a UI panel (500 x 300 pixels) and inside this panel a text field (480x280 pixels, centered inside the panel)
Furthermore I insert a script (code follows) that shall write some raycast information into the textbox.
Set predefined main camera transform data to “something” i.e. to…
position.x = 1.08, position.y = 0.94, position.z=-0.6
Rotation.x = 31.9912, Rotation.y = 328.213, Rotation.z = 10.5431
Scale.x = 1, Scale.y = 1, Scale.z = 1
so you can see the 1x1-field next to our coordinate sytem origin.
Now, when I build and run a windows exe file, I can choose the screen resolution.
My physical screen resolution is 1366 * 768 pixels. If I choose this option and move mouse cursor, the script tells me the data I expect, which means: I get the expected world coordinates.
But if I choose another screen resolution, i.e. 1024 * 768 pixels, I get data that is not corresponding to the data I expect.
Worse: if I move the mouse cursor over the UI elements, the script seems not to find the UI element at the right place (and in the right size).
If you have any suggestions what I am doing wrong, please let me know. Maybe I have to do something with camera settings, but I don’t know. It looks like Unity is using my physical screen resolution for raycast, but my chosen screen resolution for showing UI elements and doing raycasts.
If it’s important to know:
I have Windows 7, 64 bit version
and use Unity Personal Edition x 64, Version 5.1.2f1
Here’s the code of the testing script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class RaycastAtPlane : MonoBehaviour
{
// Set textbox in editor to get debugging information.
public Text textbox;
static Plane ground = new Plane (Vector3.up, Vector3.zero);
void Start ()
{
}
void Update ()
{
/*
* First get the point, but only if not inside a UI element.
*/
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()) {
/*
* mouse is pointing onto an UI element.
*/
textbox.text = "Pointing on UI element";
return;
}
float enter;
/*
* alternate solution: GetComponent<Camera>...
*/
Camera cam = Camera.main;
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
if (ground.Raycast (ray, out enter)) {
int screenWidth = Screen.width;
int screenHeight = Screen.height;
Vector3 p = ray.GetPoint (enter);
textbox.text =
"Screen width = " + screenWidth
+ "\nScreen height = " + screenHeight
+ "\nPlane x = " + p.x
+ "\nPlane y = " + p.y
+ "\nPlane z = " + p.z
+ "\nScreen x = " + Input.mousePosition.x
+ "\nScreen y = " + Input.mousePosition.y
+ "\nScreen z = " + Input.mousePosition.z;
} else {
textbox.text = "Not pointing to plane.";
}
}
}