Drag Objects with mouse?

Hi,

I want to drag Objects with my mouse and I made this allready:

using UnityEngine;

public class DragAndDropRaycast : MonoBehaviour {

    Collider SelectedCollider;

	void Update () {

        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit Hit;

            if (Physics.Raycast(ray, out Hit, Mathf.Infinity))
            {
                if (Hit.collider.tag == "selectable")
                {                
                    SelectedCollider = Hit.collider;
                }
            }
        }

        if (Input.GetMouseButton(0))
        {
            SelectedCollider.transform.position = ??? //What comes here?
        }
	}
}

I don’t know what to add there… I tried so much now.
Please keep it simple.

Thanks,

Jonas

Really old, but really useful tutorial.

I found a solution :smiley:

After geting the collider with

SelectedCollider = Hit.collider; 

I move the collider with:

SelectedCollider.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 9f)); //IMPORTANT: the 9 is the Y axis in the world

I hope I can help someone else :smiley: