First person Raycast radius

I got a script that works where when you press a keypad with the crosshair, you open the keypad menu.

The problem is that when I use this script, I don’t know how to control the radius of how far you can click from. You can click from all the way across the room. I want there to be a certain radius where you can click it from.

How can I do this?

Here is the script:

if (Input.GetMouseButtonDown(0))
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out var hit)) // This is where I think the problem is.
    {
        if (hit.transform.name == "Keypad #1")
        {
            KeypadEnabled= true;
            Code = "1234";
            print("My object is clicked by mouse"); 
        }
    }
}

You can pass a max distance parameter into Raycast. For the version of Raycast you are using, every parameter after out is optional and maxDistance defaults to infinity.

float distance= <whatever you want>;
Physics.Raycast(ray, out var hit, distance);

Thanks! This worked. But adding var between out and hit didn’t work. But adding a distance variable did. Thanks!