Raycasting with Oculus Integration / Measuring the distance

Hello,

I want to measure the distance between two points. The points are targeted by the user by looking at point A, pressing alpha1 on the keyboard and B, pressing alpha2 on the keyboard.
I got this far:

#pragma strict
 
private var measuring = false;
private var startPoint : Vector3;
private var dist;

function Update() {
    var hit : RaycastHit; 
    if (Input.GetKeyDown(KeyCode.Alpha1)) {
        dist = 0.0f;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // if (Physics.Raycast(transform.position, transform.forward, hit, 10)) {
           measuring = true;
           startPoint = hit.point; 
        }
        
    }
    if (measuring && Input.GetKeyDown(KeyCode.Alpha2)) {
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // if (Physics.Raycast(transform.position, transform.forward, hit, 10)) {
         dist = Vector3.Distance(startPoint, hit.point);
        }
    }
}

function OnGUI() {
    if (measuring) {
        GUI.Label(Rect(50,50,180,50), "Distance: " + dist.ToString());
    }
}

My problem is, that this code only works with the standard main camera object, but I want to use the Oculus integrated OVRCameraRig.
I get the following exception:

NullReferenceException: Object reference not set to an instance of an object
MeasureInGame.Update () (at Assets/MeasureInGame.js:11)

I found a solution on this site: https://kaharri.com/unity-gaming-shootingaiming-part3-oculus/
I created a ShotSpawner object as a child of OVRCameraRig (this should act like a gun in front of the camera) and changed the Raycast to

Physics.Raycast(transform.position, transform.forward, hit, 10) 

to get the users view.
But it also doesn’t seem to work.

How can I get the aiming done with the Oculus a Main Camera. And is it correct that I strictly need to have a collider on my objects to be measured or is there a solution without collider?

Greetings

It works with the “Main Camera” because you’re using “Camera.main”.

You just have to click-drag the oculus’ camera into a variable. From memory, it will be one of the eyes, L or R.

public Camera oculusCam;

Then use:

oculusCam.ScreenPointToRay(...