Accuracy in selecting objects, using raycasting

I am currently working on a browser-based board game, and it is coming along fine. We are a couple weeks from entering a testing and fixing bugs phase, and something isn’t right with how I select my pawns to move around. Some of the times, they are selected with one click and sometimes it takes double or multiple clicks to select pawns. Same thing goes for when trying select a moveable location.

Here is a code sample on how I am selecting pawns:

function CheckIfPawnSelected() {
	if(Input.GetMouseButtonDown(0)) {
		var ray = camera.ScreenPointToRay(Input.mousePosition);
		var hits : RaycastHit[];	
		hits = Physics.RaycastAll(ray, 200, -1);

		for(var i=0; i < hits.length; i++) {
			var hit : RaycastHit = hits[i];
			for(var pawnsIndex = 0; pawnsIndex <= 8; pawnsIndex++) {
				if(hit.transform.position == boardManager.pPawn[pawnsIndex].transform.position)
					pawnNumber = pawnsIndex;
			}
		}
	}
}

And a view of a testing scene for a visual:

On a side note, does anyone know why this happens?

When I sometimes open scripts in Unitron they will change the casing of the scripts. Just a minor frustration that I needed to vent.

Is your ray length long enough? Are you hitting pawns that are invisible?

Can you simplify your code a bit by using Layers to cast only against pawns ? It should be faster to as you can use Raycast rather than RaycastAll.

Yes the length is long enough. My camera is no more than 60 units away from the center of my board at any point in time.

No pawns are invisible.

Using Raycast vs RaycastAll produces the same effect. Although, you’re right, I don’t need RaycastAll for pawn selection - thank you.

Is there another way besides raycasting, to select objects on screen? Or, is there a way to increase the diameter or density of the ray you cast? I have a build uploaded on a site, but I can’t post it publicly. If anyone cares to take a peak and see what’s going on, please PM me.

If there is a Collider on the GameObject you can use OnMouseDown(). It just won’t work on iPhone.

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

That is unbelievably much more accurate for my application. Thank you.