How to cast a limited Ray?

Hi,

I whant to cast a limited Ray like my Debug.DrawRay example shows.

using UnityEngine;
using System.Collections;

public class TestViewOfRay : MonoBehaviour
{
	public float length = 2;
	void Start ()
	{
	
	}
	void Update ()
	{
		Vector3 forward = Camera.main.transform.TransformDirection (Vector3.forward)*length;
		Debug.DrawRay (Camera.main.transform.position , forward, Color.green);
	}
}

It should recognice every collider near 2 units and with the tag namex.
this is the closest i came to the debug one…

using UnityEngine;
using System.Collections;
private string text = "hi";

public class Name : MonoBehaviour
{
        private bool display = false;
	    public float rayLength = 2;

	    void Update ()
	    {
		
    	Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
		RaycastHit hit;
		
		if (Physics.Raycast(ray, out hit))
		{
			if (hit.collider.transform.tag == "Namex")
			{
			    text = hit.collider.transform.gameObject.name;
			    display = true;

			}
			if (hit.collider.transform.tag != "Namex")
			{
				display = false;
			}
		}
	}
	void OnGUI ()
{
	if (display == true)
	{
		GUI.Box (new Rect(Screen.width/2,Screen.height,30,50),text);
	}
}
}

I’m writing in C#, am currently using the standard asset First Person Controller, am not getting the 2 unit distance and the Gui Box hi is spassing sometimes in different angles.
I tried a bunch of raythings but I am pretty new to them and getting a little nuts because I cant figure out a solution… :confused:

I Would be glad about some help :smiley:

Thanks in advance,

T-low

static bool Raycast(Ray ray, RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);