Vector 3 distance using ScreenPointToRay

Hello, i found this script which makes my camera able to grab objects that have a rigidbody component, the only problem is that i can grab the objects from really really far away and i dont know how to use a float to tell unity how long the ray has to be. Here’s the script:

using UnityEngine;
using System.Collections;

public class PickupObject : MonoBehaviour {
	GameObject mainCamera;
	bool carrying;
	GameObject carriedObject;
	public float distance;
	public float smooth;

	// Use this for initialization

	void Start () {
		mainCamera = GameObject.FindWithTag("MainCamera");
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if(carrying) {
			carry(carriedObject);
			checkDrop();
			//rotateObject();
		} else {
			pickup();
		}
	}

	void rotateObject() {
		carriedObject.transform.Rotate(5,10,15);
	}

	void carry(GameObject o) {
		o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
		o.transform.rotation = Quaternion.identity;
	}

	void pickup() {
		if(Input.GetKeyDown (KeyCode.E)) {
			int x = Screen.width / 2;
			int y = Screen.height / 2;

			Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3 (x, y));
			RaycastHit hit;
			if(Physics.Raycast(ray, out hit)) {
				Pickupable p = hit.collider.GetComponent<Pickupable>();
				if(p != null) {
					carrying = true;
					carriedObject = p.gameObject;
					//p.gameObject.rigidbody.isKinematic = true;
					p.gameObject.GetComponent<Rigidbody>().useGravity = false;
				}
			}
		}
	}

	void checkDrop() {
		if(Input.GetKeyDown (KeyCode.E)) {
			dropObject();
		}
	}

	void dropObject() {
		carrying = false;
		//carriedObject.gameObject.rigidbody.isKinematic = false;
		carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
        carriedObject.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
        carriedObject.gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
        carriedObject = null;

	}
}

I think that the part were i can tell the ray lenght is here

Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3 (x, y));

thank you for your help.

Instead of Physics.Raycast(ray, out hit)at line 44, try Physics.Raycast(ray, out hit, 5f)

Adjust the float (5f) to your liking. Hope this helps