Skip to content

Instantly share code, notes, and snippets.

@YoungjaeKim
Created July 20, 2013 15:21
Show Gist options
  • Save YoungjaeKim/6045432 to your computer and use it in GitHub Desktop.
Save YoungjaeKim/6045432 to your computer and use it in GitHub Desktop.
GameObject Drag and Drop action for Unity3D
using UnityEngine;
using System.Collections;
public class DragAndDrop : MonoBehaviour
{
private bool _mouseState;
public 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;
if (Target == GetClickedObject (out hitInfo)) {
_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;
}
}
@AbhayaAgrawal1
Copy link

hey ! your script helped me a lot. I have one problem regarding drag and drop only can you help me?

@pratikone
Copy link

Something which worked. Thank you.
I didn't know where to add this script so I added it to my floor plane of the scene.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment