Created
July 20, 2013 15:21
-
-
Save YoungjaeKim/6045432 to your computer and use it in GitHub Desktop.
GameObject Drag and Drop action for Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.