Dragging an Object With Mouse

Hi there,

I’d like to create a plane to be used as a menu and have it so it can be dragged up and down. I’ve looked around for answers and the problem with all the ones that I have seen is that the object resets to the mouse position.

I’ve tried different solutions but can’t seem to get it working. I was wondering if anyone might have any suggestions. Preferably in c#.

Thanks for your time.

If you don’t want it snapping to the mouse’s position when clicked on, then when the OnMouseDown() event is called, store its the relative offset of the point clicked and the collider’s pivot using:

RaycastHit hitInfo;
if (yourObjectsCollider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hitInfo,Mathf.Infinity)) {
    offset = hitInfo.point - hitInfo.transform.position; //offset should be a member variable since youll probably be using it during OnMouseDrag()
}

Then wherever you’d set the collider’s position in the scripts you mentioned you’ve tried, you’d just add offset.