drag and drop in unity 2d code example
Example: 2d item dragging unity
public class followMouse : MonoBehaviour
{
bool canDrag = false;
Vector3 itemPos;
void Update()
{
transform.position = Input.mousePosition;
if (!Input.GetMouseButton(0))
{
canDrag = false;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("itemIcon"))
{
if (Input.GetMouseButton(0))
{
canDrag = true;
other.transform.position = Input.mousePosition;
}
}
}
}