Drag drop game objects (without rigidbody) with the mouse

I’ve searched and read, but haven’t found any satisfactory answers…

Lets say I have a clean project. I add a cube. When pressing play in Unity3D I want to move this cube with my mouse. Pseudo code:

function OnMouseDrag() {
  // move gameObject along with the mouse
  // transform = Mouse.transform ??
}

There are no rigid bodies involved, so I’m guessing this should be fairly simple without a lot of code?

2 Likes

Try adding this to the object you want to drag

//Script to drag an object in world space using the mouse


var screenSpace;
var offset;

function OnMouseDown(){
	//translate the cubes position from the world to Screen Point
	screenSpace = Camera.main.WorldToScreenPoint(transform.position);
	 
	//calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
	offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
	
}

/*
OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseDrag is called every frame while the mouse is down.
*/

function OnMouseDrag () {

	//keep track of the mouse position
	var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);     

	//convert the screen mouse position to world point and adjust with offset
	var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

	//update the position of the object in the world
	transform.position = curPosition;
}
1 Like

Perfect! Thanx :slight_smile:

For others who might get help like me, I changed code to C# version.
(I’m Unity beginner so codes are verbose.)
I tested it and it works.

using UnityEngine;
using System.Collections;

public class DragAndDrop : MonoBehaviour
{	
	private bool _mouseState;
	private GameObject target;
	public Vector3 screenSpace;
	public Vector3 offset;

	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		// Debug.Log(_mouseState);
		if (Input.GetMouseButtonDown (0)) {

			RaycastHit hitInfo;
			target = GetClickedObject (out hitInfo);
			if (target != null) {
				_mouseState = true;
				screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
				offset = target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
			}
		}
		if (Input.GetMouseButtonUp (0)) {
			_mouseState = false;
		}
		if (_mouseState) {
			//keep track of the mouse position
			var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

			//convert the screen mouse position to world point and adjust with offset
			var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;

			//update the position of the object in the world
			target.transform.position = curPosition;
		}
	}
	
	
	GameObject GetClickedObject (out RaycastHit hit)
	{
		GameObject target = null;
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
			target = hit.collider.gameObject;
		}

		return target;
	}
}
5 Likes

Does that work for 2D aswell?

Thank you so much mole420 and YoungjaeKim for that code. Works like a charm.

1 Like

Hey I think I found a better solution at least for 2D! :slight_smile:

void OnMouseDrag(){
       
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = 0f;
            transform.position = mousePos;
        
    }
2 Likes

No need to call z = 0, because Input.mousePosition is a Vector2 being used to make a Vector3, so z is going to be zero regardless. Also, this is the exact same thing posted as one of the first responses, except you use Input.mousePosition directly for ScreenToWorldPoint instead of converting it to a Vector3 first (ScreenToWorldPoint at the time that the original post was made might not have accepted Vector2s as a parameter, maybe).

Also, please don’t necro threads without a good reason.

I want drag game object only in fixed path.so any one can help me…

have you tried just setting one of the values for a move axes to 0

u prolly dont care anymore tho so nvm

You are awesome YoungjaeKim, that script rocks! Thank you very much!

do you have it with rigid body

Please look at the dates of posts before replying to them.

Let’s close this necro magnet of a thread. :slight_smile:

2 Likes