Raycasting to pick up items

Hey,

I’ve been trying to create a simple script where a a ray is casted to check for an item and if the item (tagged) is lets say within 2m a GUI is enabled prompting you to pick it up. Now, I can do the whole GUI and stuff but not raycasting. I’ve downloaded some scripts to see if I can work it out myself but no luck. I’ve checked the Script Reference but I just can’t seem to work it out. If anyone could help that would be great cheers.

Assuming your object to be picked has tag “Pick” and this is a 3d environment. Try this

void Update()
 {
    var fwd = transform.TransformDirection(Vector3.forward);
	var hit : Raycast;
	if (Physics.Raycast(transform.position, fwd,  hit))
	{
		if( ( hit.distance <= 2.0 ) && ( hit.collider.gameobject.tag == "Pick" ) ) 
		{
			// show pick GUI
		}
	}
}

So I have another problem. After drawing the ray it only went forward. So that won’t work if there’s a small item on the ground. This would work:

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

	if (Physics.Raycast (ray, 100)) {
		print ("Hit something");
	}
}

It’s not fully what I want because I would only want for something to print if it’s a packable item not just if it hits something.