[STILL UNANSWERED] Lock object to mouse position & rotate it about mouse? [C#, 2D]

So I have a rigidbody2d on a component. I want it to move with the mouse like this:


How would I do this?

Current Code:

GameObject controller;
    public LayerMask hitLayers;
    Rigidbody2D rig;

    bool selected;

    private void Start()
    {
        controller = GameObject.Find("Controller");
        rig = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        
            if (selected)
            {
                Vector3 pos = Input.mousePosition;
                pos.z = 10;
                pos = Camera.main.ScreenToWorldPoint(pos);
                Vector3 dist = pos - transform.position;
                Vector3 newPos = pos - dist;
                transform.position = newPos;
            }
        
    }

    private void OnMouseUp()
    {
        selected = false;
        rig.velocity = new Vector2(0, 0);
        rig.angularVelocity = 0;
    }

    private void OnMouseDown()
    {
        selected = true;
    }

But it doesn’t move at all. When you click on it, it will stop falling and very slowly move down, and if its already landed, it won’t do anything.

You are going to want to do a simple raycast to pickup the object and use a TargetJoint2D component to have the object anchor based on your mouse position relative to the object and track your mouse position. This is a basic working version. I would look into more raycasting stuff as it is extremely useful to know how to do. Hope this helps!

Attach this script to your box with the rigidbody2D and its box collider.

[RequireComponent(typeof(TargetJoint2D))]
public class Dragable : MonoBehaviour 
{
	private TargetJoint2D _joint;

	void Start () 
	{
		_joint = GetComponent<TargetJoint2D> ();
		if (_joint == null) {
			enabled = false;
		} else {
			_joint.enabled = false;
		}
	}

	public void BeginDrag(Vector2 mouse_pos)
	{
		Vector2 pos = (Vector2) transform.position;
		Vector2 scale = (Vector2)transform.localScale;
		Vector2 anchor = (mouse_pos - pos);
		if (scale.x == 0) {
			anchor.x = 0f;
		} else {
			anchor.x /= scale.x;
		}
		if (scale.y == 0) {
			anchor.y = 0f;
		} else {
			anchor.y /= scale.y;
		}
		_joint.enabled = true;
		_joint.anchor = anchor;
		UpdateDrag (mouse_pos);
	}

	public void UpdateDrag(Vector2 mouse_pos)
	{
		_joint.target = mouse_pos;
	}

	public void EndDrag()
	{
		_joint.enabled = false;
	}
}

Attach this script to your camera, or just whatever object since it doesn’t rely on the object it is attached to but make sure there is only one of them in the scene at a time.

public class BoxDrag : MonoBehaviour 
{
	private Dragable _drag_target;

	void Start()
	{
		_drag_target = null;
	}

	void Update () 
	{
		if (_drag_target == null) {
			if (Input.GetMouseButtonDown (0)) {
				Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint (Input.mousePosition);
				RaycastHit2D hit = Physics2D.Raycast (mouse_pos, Vector2.zero);
				if (hit.collider != null) {
					Dragable dragable = hit.collider.GetComponent<Dragable> ();
					if (dragable != null) {
						_drag_target = dragable;
						_drag_target.BeginDrag (mouse_pos);
					}
				}
			}
		}
		if (_drag_target != null) {
			Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint (Input.mousePosition);
			_drag_target.UpdateDrag (mouse_pos);
			if (Input.GetMouseButtonUp (0)) {
				_drag_target.EndDrag ();
				_drag_target = null;
			}
		}
	}
}

When pressing down the mouse shoot a raycast and see if the mouse is over the cube, if so have the cube follow the mouse until you release the mouse. you can mainly do this with simply if statements like:
if(Input.GetButton(“Fire1”)) {

}