Grid based movement question

I’m not at home right now to test this theory I have so I’ll ask here. A little background first. In an effort to help me learn Unity3D better I am taking a game I created in flash and trying to recreate it. Its a 2D grid based strategy game which I created for the Ludum Dare a couple of years back.

My background sprite is 300x300 and the select box sprite is 20x20 so my grid is 15x15. When you move the mouse, which is linked to the select box, your only able to move the box within that 15 by 15 grid. I’ve gotten this to work but I’m having difficulty with it moving every 20 spaces like it does in the flash version.

My theory is this:

mPos = Camera.main.ScreenToWorldPoint(input.mousePosition);
x = mPos.x;
y = mPos.y;

if(x % 20 = 0 && y % 20 = 0)
{
      transform.position = mPos;
}

I will be the first to admit I’m still learning so the above code may have errors in it. Am I on the right track? Will this does what I want it to do?

-KunoNoOni

What you have right now requires your mouse to be exactly on a coordinate that is divisible by 20.
What I think you want is something like this:

Vector3 position = new Vector3();
position.x = mPos.x - (mPos.x % 20);
position.y = mPos.y - (mPos.y % 20);

this.transform.position = position

Ah yes you are correct. that did the trick, thank you. I just need to tweak it now.

-KunoNoOni