How to limit this particular raycast distance?

I need to make the rays distance 2 if the playerpref Wep = 1. I have tried but i have failed alot please help me!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerShootingAss : MonoBehaviour {

	public float fireRate = 0.5f;
	public float cooldown = 0;
	public float cooldown2 = 0;
	public float damage = 25;
	FXManager fxManager;
	private int Wep;
	public bool cooldownChange = false;
	public bool cooldownChange2 = false;
	public Image reloaddefault;
	public Image reloadsniper;
	void Start(){
		fxManager = GameObject.FindObjectOfType<FXManager>();

		if (fxManager == null) {
			Debug.LogError ("couldn't find an fx manager");
		}
		reloaddefault = transform.FindChild("FirstPersonCharacter").FindChild ("Laserbow").FindChild ("PlayerCanvas").FindChild ("HealthBGgun1").FindChild ("Health").GetComponent<Image> ();
		reloadsniper = transform.FindChild("FirstPersonCharacter").FindChild ("Sword").FindChild ("PlayerCanvas").FindChild ("HealthBGgun2").FindChild ("Health").GetComponent<Image> ();
	}



	void Update () {
		Wep = PlayerPrefs.GetInt ("Wep");
		cooldown -= Time.deltaTime;
		cooldown2 -= Time.deltaTime;
		reloadsniper.fillAmount = (float)cooldown2 / (float)fireRate;
		reloaddefault.fillAmount = (float)cooldown / (float)fireRate;

		if (Input.GetButton ("Fire2")) {
			transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(false);
			transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (false);
		} else { 
			transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(true);
			transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (true);
		}

		if (Input.GetButton ("Fire1")) {
			Fire ();
		}
		if (Wep == 0) {
			fireRate = 3f;
			damage = 100;
		} else {
			fireRate = 1f;
			damage = 50;
		}
		if (Input.GetButtonDown("Wep1")) {
			cooldownChange = true;
		}
		if (Input.GetButtonDown("Wep2")) {
			cooldownChange2 = true;
		}
		if (cooldownChange == true){
			cooldownChange = false;
		}
		if (cooldownChange2 == true){
			cooldownChange2 = false;
		}
	}

	void Fire(){
		if (Wep == 0) {
			if (cooldown > 0) {
				return;
			}
		} else {
			if (cooldown2 > 0) {
				return;
			}
		}



		Debug.Log ("We shot");

			Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);

		Transform hitTransform;
		Vector3 hitPoints;

		hitTransform = FindClosestHitObject (ray, out hitPoints);

		if (hitTransform != null) {
			Debug.Log ("We hit: " + hitTransform.name);
			//hitInfo.point

			Health h = hitTransform.GetComponent<Health> ();

			while (h == null && hitTransform.parent) {
				hitTransform = hitTransform.parent;
				h = hitTransform.GetComponent<Health> ();

			}

			if (h != null) {
				h.GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.All, damage);
			}

			if (fxManager != null) {
				DoGunFX (hitPoints);

			}
		} else {
			if (fxManager != null) {
				DoGunFX (hitPoints);
				WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
				hitPoints = wd.transform.position + (wd.transform.forward*100);

			}
		}

		if (Wep == 0) {
			cooldown = fireRate;
		} else {
			cooldown2 = fireRate;
		}
	}

	void DoGunFX(Vector3 hitPoints){
		WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
		fxManager.GetComponent<PhotonView> ().RPC ("SniperBulletFX", PhotonTargets.All, wd.transform.position, hitPoints);
	}

	Transform FindClosestHitObject(Ray ray, out Vector3 hitPoints) {

		RaycastHit[] hits = Physics.RaycastAll (ray);

		Transform closestHit = null;
		float distance = 0;
		hitPoints = Vector3.zero;

		foreach (RaycastHit hit in hits) {
			if (hit.transform != this.transform && (closestHit == null || hit.distance < distance)) {

				closestHit = hit.transform;
				distance = hit.distance;
				hitPoints = hit.point;
			}
		}

		return closestHit;

	}
}

It looks like you are getting your ray using the camera transform and that transforms “forward”. The value returned will be a Vector3 where each value is between 0 and 1. Drawing that ray, it will always be 1 unit long. To make it 2 units long, multiply it by 2.0

     if (Wep == 1) {
          Vector3 val = Camera.main.transform.forward*2.0f
          ray = new Ray (Camera.main.transform.position,val );
        }

The length of the Ray direction is not relevant. The Physics.RaycastAll method has an optional parameter where you can specify the max distance. If you don’t pass any distance (like you did) the ray will have infinite length.

Fixed final code

RaycastHit hits = Physics.RaycastAll (ray);
if (Wep == 0) {
hits = Physics.RaycastAll (ray);
} else {
hits = Physics.RaycastAll (ray, 2f);
}