Rather than continuously checking wether or not you are touching the object, you should set a boolean to true, similar to this:
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, Mathf.Infinity, playerLayerMask))
{
dragging = true;
}
}
and then set dragging to false when you release the mouse. (Input.GetMouseButtonUp(0))
While dragging, have the object follow your mouse as you did in your code.
Note - in case you're wondering, this also works for mobile touch.
↧