How to use Camera.ScreenToWorldPoint in AR Foundation

I am trying to use a non-centered crosshair in an AR app as shown here:
6142479--670383--upload_2020-7-28_11-51-58.png

I am using Vector3 cameraPoint = camera.ScreenToWorldPoint(new Vector3(cursorPos.x, cursorPos.y, 0.2f));, where cursorPos is the screen position of the cursor. However, when I make a Raycast to detect collisions using cameraPoint as the ray origin, the origin position does not seem to be at cursorPos. Instead, the Raycast origin seems to be very close to the center of the camera. The code for the Raycast is ```
Physics.Raycast(cameraPoint, camera.transform.forward, out RaycastHit hit, Mathf.Infinity, layerMask)

1 Like

In my experience it’s always simpler to use Rayast version that accepts Ray:

var ray = camera.ScreenPointToRay(cursorPos);
Physics.Raycast(ray, out var hit, Mathf.Infinity, layerMask);

I also prefer KirillKuzyk’s version, that should handle everything.

Just to clarify where your problem was:
Your ray always shoots in perfect camera.forward direction. Unless you have an orthographic camera this is only correct for the camera center. On perspective cameras the ray direction points more outward the further you are from the center.

reference the original ARCamera then use ARCamera.ScreenToWorldPoint
the camera.ScreenToWorldPoint doesnt seem to work properly for AR apparently

or just modify this, replace the touch position with your cursorPos and test

I was trying to solve a similar problem, but never found ARCamera.ScreenToWorldPoint in ARFoundation 3.1.3? Is this available in the latest ARFoundation by any chance?

You can get the reference to AR camera from ARSessionOrigin:
var arCamera = FindObjectOfType().camera;

1 Like