mouse drag

how to do mouse click and drag a game object in the game.I want a simple click and dragging
a game object using a mouse.I want only 2D movement(X and Y movement only). I tried many script in previous answer it is not working for me. I prefer c#

To calculate the world coordinates for a mouse click on screen, you can use ScreenToWorldPoint from your camera. So in order to do this, you would do:

Vector3 mousePointerWorldPos = Camera.main.ScreenToWorldPoint( Input.mousePosition);

Input.mousePosition returns x,y coordinates in pixel values, and ScreenToWorldPoint converts this into world space. After that, it would be as simple as making your gameobject that you clicked on following these coordinates during update:

transform.position = mousePointerWorldPos;

There would obviously have to be checks on what gameObject you’re clicking on, and simple logic to move while the mouse button is down, etc.