Accurate Mouse Snap to Grid within Threshold? (Float Rounding Errors)

Im having the same issue as this guy but the convo ended short with no solutions. (

I know I need to add a threshold so my mouse can stay within a grid scale of 1 but not sure how to programmably approach it.

I could just fix this issue by making the mouse hidden, but Id really like to see how this can be done in a proper way. I really like the challenge but Ive spent about a day just thinking this through and Im baffled.

I dont know how to explain it further but maybe this image will help. Basically I want the mouse to hover over the entire (1 unit scale) grid. Im aware the behavior is due to rounding of Floats to Ints and that I would need a threshold to “clamp” the mouse to a 1x1 scale grid.

		Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition) ;
		Vector3 mousevector3 = new Vector3 (mouseray.origin.x, mouseray.origin.y, mouseray.origin.z);

		//apply grid to mouse hover
		if (Physics.Raycast (mouseray.origin, mouseray.direction, out projectorOrigin, 100)) {
			//snap to grid 1x1
			Vector3 gridpos = new Vector3(Mathf.Floor (projectorOrigin.point.x), Mathf.Floor (projectorOrigin.point.y), Mathf.Floor (projectorOrigin.point.z));
			grid.transform.position = new Vector3 (gridpos.x, gridpos.y, gridpos.z);


alt text

*******Notice how my mouse only moves within the UpperRight of the main box of the actual grid.

Solved it.

Rather than Mathf.Round use a function such as this. Make sure to add the variable snapValue!

    private float snapValue = 1; //in my case

	private float Round( float input )
		return snapValue * Mathf.Round( ( input / snapValue ) );


alt text

Sigh, I always solve my issues 10min after I post on here…