News: Enjoy our Christmas offer 30% off on our latest package Tower Defense Level Kit http://u3d.as/m7h

  • May 21, 2018, 02:56:23 AM

Login with username, password and session length

Author Topic: Drag and object across scenes  (Read 5059 times)

Offline jorge.acuna

  • Newbie
  • *
  • Posts: 2
Drag and object across scenes
« on: September 21, 2016, 06:13:34 PM »

  Hello,

  I have a vertical scroll with 2d cards, I want to be able to when I touch a card, place a 3d model on top of it, and make it a children of the card so it can scroll up and down accordingly.  I have already achieved the 3d parenting, but for some reason although in the easytouch event delegate I detach the 3d object so I can drag it, I can't, Only when the 3d object its unparented in the scene I can drag it with a little delay and have observed as well how the object is repositioned in the hierarchy where easytouch manager is and then it works I can drag it.

  So: there is a solution to drag a 3d object in screen space? Also, this can be done smoothly when the object its in another scene, and leave it there in the other scene? For better understanding of the case, the project loads like 5 scenes in async mode hooking to the server, and then starts to run with all the other scenes elements in their proper hierachies.

  Thanks in advance for the help!

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Drag and object across scenes
« Reply #1 on: September 23, 2016, 10:12:53 AM »
Hi,

There are several points we'll start with the simplest.

Drag in screen space :
The touches informations are in 2D relative to the resolution of the screen, detecting 2D or 3D elements is effected by a 3D raycast according to the 2D position of the touch.However, for a 2D position (screen space), there is an infinity 3D position according to the depth.So if you want to move an object in the 3d world relative to a 2d position you must use some specific method to correctly setup the Z of your object. To do this you have  these methods of gesture class (pass as parameter for each event) :

These methods use ScreenToWorldPoint : https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

Gesture.GetTouchToWorldPoint(Float z) => This method allows you to get the 3D world position relative the current touch by indicating the Z length relative to the camera

Gesture.GetTouchToWorldPoint(Vector3 position) => This method allows you to get 3D world position relative to the current touch and a 3d world position

Here a simple example on how to move an object relative to touch position
Code: [Select]
void On_Drag(Gesture gesture){

// Verification that the action on the object
if (gesture.pickedObject == gameObject ){

// the world coordinate from touch
transform.position = gesture.GetTouchToWorldPoint(gesture.pickedObject.transform.position);
                }
}

It's up to you to add smooth or better management to take into account the delta position relative to the center of the gameobject. You can look at the script DragMe.cs where the delta relative to the center of the object is managed.


leave an object in other scene
I am not a specialist in multi scene, so I find it very convenient to create very big level to position objects correctly to the previous scene to have smooth transition with asynchrous load level, but  the usefulness of this technique on single games left to the discretion of each, to judge the real interest in static level games. Personally, I think it complicates things

It's possible to move a GameObject from its current scene to a new scene this method :
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html

On this point, it's not too EasyTouch, but the possibilities of Unity and how to properly manage PLAYMODE with complex operations, which are not simple level asynchronous loading.

I invite you to study these two links, and if you find faults in EasyTouch I would fix them as quickly.
https://docs.unity3d.com/Manual/MultiSceneEditing.html
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

Nicolas



« Last Edit: September 23, 2016, 10:15:54 AM by Nicolas »