Converting screen mouse position to local mouse position between -1 and 1
<p>I have a 2D tile game project in Unity where every tile has a size of (for example) 32 x 32 pixel but in the grid the tiles are mapped to full integer coordinate, so for example <code>tile0</code> is at <code>x0 y0</code>, <code>tile1</code> is at <code>x1 y0</code>, <code>tile2</code> is at <code>x2 y0</code>, <code>tile10</code> is at <code>x2 y1</code>, and so on (if I'm not mistaken this mapping is due to setting the <code>pixel per unit size</code> in the sprite sheet to e.g. 32px, right?)</p>

<p>In any case, the player should be able to drag tiles left/right/up/down with the mouse and I want the tile to be dragged along with the mouse position. The problem I'm having is that I don't know how to translate the global mouse coordinate into the local space of the dragged tile, and in particular into a range between -1 and 1 because the drag distance is normalized to that range.</p>

<p>In my <code>Update()</code> method I have this code:</p>

<pre><code>if (_isMouseDown &amp;&amp; sourceTile != null)
{
    _isDraggingTile = true;
    Vector3 dragDistance = Input.mousePosition - _originMousePosition;
    dragDistance.Normalize();

    GridCell targetCell;

    /* Check mouse up/down drag. */
    float f = Vector3.Dot(dragDistance, Vector3.up);
    if (f &gt;= 0.5f)
    {
        targetCell = sourceTile.gridCell.upNeighbor;
    }
    else if (f &lt;= -0.5f)
    {
        targetCell = sourceTile.gridCell.downNeighbor;
    }
    else
    {
        /* Check mouse left/right drag. */
        f = Vector3.Dot(dragDistance, Vector3.right);
        targetCell = f &gt;= 0.5f ? sourceTile.gridCell.rightNeighbor : sourceTile.gridCell.leftNeighbor;
    }

    if (Vector3.Distance(_originMousePosition, Input.mousePosition) &gt; _globals.tileDragThreshold)
    {
        if (targetCell != null)
        {
            SetTargetTile(targetCell.tile);
            ResolveMove();
        }
    }
}
</code></pre>

<p>The code checks into which direction the mouse is dragged and after a certain threshold switches the dragged tile with the one in the adjacent neighbor grid cell (<code>targetCell</code>). What i'd like to achieve is that the dragged tile follows the mouse position until the swap happens.</p>

<p>Could anyone give me a hint on how to get the mouse position converted into this so to adjust the dragged tile's position accordingly?</p>