Comparing Return Value of Mathf.Abs() Produces Wrong Result

Using Unity Developer Preview Version 3.5.0b6, MonoDevelop v2.8.2, Scripting with JavaScript.

I have a 3 dimensional 6x8 grid of cubes scaled at (1,1,1), and spaced 1.08 apart on the x and z dimensions. The player moves from cube to cube but is limited to one cube per turn in any direction; including diaginally. To validate the move I am taking the transform.position.x, and transform.position.z coordinates for both the current cube, and the selected cube and determining if either the x or z deltas are greater then the spacing. If so it would indicate that the user selected a cube beyond the limitation of one in any direction.

Everything works correctly until the user selects a cube that is in the -x direction, based on the code below the result will be a negative number. So I use Mathf.Abs() to make all the values positive. But it still returns the wrong result if the user chooses any cube in the negative direction. selections in the both z dirctions seem to work properly.

Am I doing something wrong here? Thanks for the help.

var gridSpacing : float = 1.08;


function isValidMove(selectedObject : GameObject) : boolean
{

	var selItemX : float = selectedObject.transform.position.x;
	var selItemZ : float = selectedObject.transform.position.z;
	
	var curItemX : float = currentPlayer.transform.position.x;
	var curItemZ : float = currentPlayer.transform.position.z;
	
	 
	var tmpValZ : float = Mathf.Abs(Mathf.Abs(selItemZ) - Mathf.Abs(curItemZ));
	var tmpValX : float = Mathf.Abs(Mathf.Abs(selItemX) - Mathf.Abs(curItemX));
					
	if((tmpValZ <= gridSpacing) == false || (tmpValX <= gridSpacing) == false)
	{
	     return false;
	}
	return true;
}

You likely don’t want to use the embedded Abs functions, as you want the magnitude of the difference.

var tmpValZ : float = Mathf.Abs(selItemZ - curItemZ);

Also, it’s unrelated, but your conditional for determining what to return is harder to read than it needs to be as the ‘== false’ portions are a bit strange. You could use this instead:

if(tmpValZ > gridSpacing || tmpValX > gridSpacing)