- Home /
Why does my render texture cursor appear offset from my mouse cursor?
Currently trying to get a scene working where I move the mouse pointer, which then moves a cursor sprite in worldspace, which is captured by a camera, which then displays the sprite and any other objects on a render texture. Project resolution size is based on Samsung TabA 2016 - 1280x800.
Unfortunately, I'm getting an odd offset of the cursor sprite from the mouse position and it's confusing the heck out of me.
When the mouse position is on the left side of the screen, the cursor sprite appears about 25% into the screen from the left hand side, and moving the mouse towards the bottom of the screen makes the cursor sprite vanish off-screen.
Could anybody help me figure out why I'm getting this offset?
[Here is a package][1] with the relevant scene, code and assets.
Thanks in advance. [1]: https://www.dropbox.com/s/scrqiuq2i5ekeog/remoteCursorPlacementRS.unitypackage?dl=0
Answer by Reverend-Speed · Dec 31, 2018 at 08:08 PM
As it turns out, the solution was to get off my ass, tear out the old code (which only relied on camera.orthographicSize), do a little basic math and... voila... fixed HitTestUVPosition code:
bool HitTestUVPosition(ref Vector3 uvWorldPosition)
{
RaycastHit hit;
Vector3 cursorPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
Ray cursorRay = sceneCamera.ScreenPointToRay(cursorPos);
if (Physics.Raycast(cursorRay, out hit, 200))
{
Debug.DrawRay(cursorRay.origin, cursorRay.direction, Color.green);
Debug.DrawRay(brushCursor.transform.position, cursorRay.direction, Color.yellow);
MeshCollider meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null) return false;
Vector2 pixelUV = new Vector2(hit.textureCoord.x, hit.textureCoord.y);
float screenAspect = (float)Screen.width / (float)Screen.height;
float camHalfHeight = canvasCamera.orthographicSize;
float camHalfWidth = screenAspect * camHalfHeight;
uvWorldPosition.x = (pixelUV.x * camHalfWidth * 2) - camHalfWidth;
uvWorldPosition.y = (pixelUV.y * camHalfHeight * 2) - camHalfHeight;
uvWorldPosition.z = 1.0f;
return true;
}
else
{
Debug.DrawRay(cursorRay.origin, cursorRay.direction, Color.red);
return false;
}
}
Easy when you eventually figure out how.