As this thread is quite high in google (and it has 1.7K views), i decided to add my answer here.
So, just in case someone will found it useful, final code is
void Update ()
{
CheckButtonInput();
if (selectedObject != null)
{
newPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
newPosition.z = 0;
selectedObject.transform.position = newPosition - selectedDelta;
}
}
void CheckButtonInput()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag == "Card")
{
Debug.Log("Found a Card at " + hit.point);
selectedObject = hit.collider.gameObject;
selectedDelta = hit.point - hit.collider.transform.position;
}
else
{
Debug.Log("Found something " + hit.collider.tag);
selectedObject = null;
}
}
else
{
Debug.Log("Found nothing");
}
}
if (Input.GetMouseButtonUp(0))
{
Debug.Log("Releasing");
selectedObject = null;
}
}