How to click and drag 2D GameObjects that can be thrown after letting go.

public float smoothSpeed;

bool isClicked = false;
Vector2 mousePos;

void Update()
{
    mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    if (isClicked == true)
    {
        transform.position = Vector2.Lerp(transform.position, mousePos, smoothSpeed);
    }
}

private void OnMouseDown()
{
    isClicked = true;
}

private void OnMouseUp()
{
    isClicked = false;
}

Currently, I’m just lerping the position of the game object and the mouse together. But a collision with other game objects is weird because I’m setting position instead of using rigidbody2d. I also want the game object to retain velocity after letting go. Any suggestions?

You could use Rigidbody2D instead:

    private Rigidbody2D rb;
    [SerializeField] private float speed;
    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    //public float smoothSpeed;
    bool isClicked = false;
    Vector2 mousePos;

    void Update()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (isClicked == true)
        {
            Vector2 direction = mousePos - (Vector2) transform.position;
            rb.AddForce(direction * speed * Time.deltaTime);
            /*
             * //Or use noremalized speed:
             * rb.AddForce(direction.normalized * Time.deltaTime * speed);
            */
        }
    }
    private void OnMouseDown()
    {
        isClicked = true;
    }
    private void OnMouseUp()
    {
        isClicked = false;
    }

Hope that helps.