How To Make GUI/Dragable Object - Att: Video


Basically, Right now I have a very simple GUI with a button. What I would like to do is to have that button to be able to be dragged to the scene, in where it will turn into a game object that follows the mouse.

Watch This To See What I Mean:

Like an inventory, but the reversal. So instead of picking up things from scene. I want to be able to drop onto scene from GUI. Preferably with a grid snap feature such as the one shown in the video.

Please help, or point me in the right direction.


Break it down:

  1. Drag and store which item was dragged
  2. Detect where it is dropped
  3. Convert drop location from screen to world coordinates
  4. Snap the 3d position
  5. Create an object based on which item was dragged

Here are some tips:

setup: Use a GUITexture so you can use OnDrag, store a reference to the prefab you want to create inside that object.

  1. store the Prefab reference associated with the button inside OnDrag in a variable visible from (5.)
  2. Use Input.GetMouseButtonUp
  3. Use Camera.ScreenToWorldPoint
  4. Use the modulus operator for x and z coordinates
  5. Use Instantiate if your object is a prefab

Oliver, instead of pasting code, I thought it would be more helpful to send you the scene I've created so that you would understand it better. So I've sent it to your email.

Im not very sure but this is experimentation that maybe will work

if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drag";
//here you put when you clic to the GUI
if ( Input.GetMouseButtonUp(0)){
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drop";
//Here you put when you clic and that construction its build

Recomendation: For this its not sure to put a GUI in the script, it will be better if you do it in the scene.