[JS] Raycast don't work very well


I already posted here but I just realized that my raycast system don’t work very well… Can you help me for that ?

I trust the GUI (function GUI) and I notice that I did not really detect collisions, according to my character position when i target an item…

Edit: Something when i look an object the GUI show “Take object A” and then, when i look another object, the gui should be show “Take the object B” but the gui show “Take the object A”.

Sorry for my bad english.

Thank you.

function OnGUI ()

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  var direction = transform.TransformDirection(Vector3.forward);
  var hit : RaycastHit;
	//This is where we draw a box telling the Player how to pick up the item.
	GUI.skin = InstructionBoxSkin;
	GUI.color = Color(1, 1, 1, 0.7);
	if (canPickUp == true)
		if (Physics.Raycast (ray, hit, 100) && hit.transform.tag == ("Pickable"))
		if (gameObject.rigidbody.isKinematic == false)
			GUI.Box (Rect (Screen.width*0.5-(165*0.5), 200, 165, 22), " " + transform.name + "");
			canPickUp = false;

Read the 1. Screen point functions required a Vector3 even though z is ignored. Also, check your mouse coordinates are not causing a problem

The bottom-left of the screen or
window is at (0, 0). The top-right of
the screen or window is at
(Screen.width, Screen.height).