Author Topic: Drag & Drop from 2D to 3D Space  (Read 3442 times)

Oinobareion

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Drag & Drop from 2D to 3D Space
« on: June 06, 2013, 05:17:08 AM »
Hello,

I am having big troubles to integrate the "Example 11 - Drag & Drop" functionality into my own project. I want to drag an object from the 2D UI and drop it on an object in the 3D space. Is there a step-by-step description on how to do it? I'll try to explain what I did so far:

- I have an Anchor that is positioned at the left side of the screen. Inside, I have a Panel and some Widgets that represent some kind of media library (= a collection of images). From this media library I want to drag 2D objects on the 3D canvas (a simple plane).

My questions: Where do i have to attach the following scripts to? DragDropItem.cs, DragDropRoot.cs, DragDropSurface.cs

Thanky for any help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag & Drop from 2D to 3D Space
« Reply #1 on: June 06, 2013, 07:26:54 AM »
Why not examine where those scripts are in the example? Just search for the script names in the Hierarchy window (case sensitive). You will answer your own question.

Don't forget to have colliders, and to disable the collider of the object being dragged while it's dragged.

Oinobareion

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Drag & Drop from 2D to 3D Space
« Reply #2 on: June 06, 2013, 10:08:35 AM »
Hi,

Ok, I took a close look now and was able to recreate the scene. There is one thing, I am still struggling with. The instantiation of the prefab happens here in AddChild.

  1.         static public GameObject AddChild (GameObject parent, GameObject prefab)
  2.         {
  3.                 GameObject go = GameObject.Instantiate(prefab) as GameObject;
  4.  
  5.                 if (go != null && parent != null)
  6.                 {
  7.                         Transform t = go.transform;
  8.                         t.parent = parent.transform;
  9.                         t.localPosition = Vector3.zero;
  10.                         t.localRotation = Quaternion.identity;
  11.                         t.localScale = Vector3.one;
  12.                         go.layer = parent.layer;
  13.                 }
  14.                 return go;
  15.         }
  16.  

I am instantiating my prefabs on a plane, which actually works, but they are "inside" the plane. I'd like them a little bit above the plane. So I tried....

  1. t.localPosition = new Vector3(0, 1, 0);
  2.  

But it didn't change anything. Am I thinking in a completely wrong way?

Thank you!

P15Studios

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Drag & Drop from 2D to 3D Space
« Reply #3 on: June 07, 2013, 01:46:05 PM »
I've been working on a similar thing and what I have found works is to create my model with the anchor point at a different location so that any model I place shows up at the floor level and not hanging through the bottom

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag & Drop from 2D to 3D Space
« Reply #4 on: June 07, 2013, 03:29:33 PM »
Local position may or may not be rotated, so Vector3.up may or may not point up. You're on the right track though. You may want to use transform.position = transform.position + Vector3.up.