2D Toolkit Forum

2D Toolkit => Support => Topic started by: test84 on December 01, 2013, 08:03:22 pm

Title: How to have a draggable object with 2D toolkit?
Post by: test84 on December 01, 2013, 08:03:22 pm
Hi,

How can I have a draggable object that has 2D toolkit sprite on it? Do I have to do anything special or it's regular objects with 2DTK componenet attached to them?

Thanks.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on December 01, 2013, 08:37:33 pm
Look into the draggable button in the tk2dUI example. Thats one way of doing it.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on December 01, 2013, 09:26:02 pm
Thanks for the reply. My game requires a lot of draggable objects, can I do it with the button? I don't want toggle and stuff.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on December 01, 2013, 09:38:02 pm
If you have a lot of them you should probably make a behaviour to handle it. The tk2dUIDragItem script will be a good place to start. The entire ui system can be tweaked, eg. turn off button highlights, etc and you have a draggable sprite.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on December 02, 2013, 01:26:19 am
Thanks but can't get my head around doing that. Do you have any other solutions? What I want is a simple drag and drop of my animated sprites.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on December 02, 2013, 06:12:33 pm
The tk2dUIDragItem scripts is about as easy as it gets without coding. All you need is a tk2dUIItem on the sprite, followed by a tk2dUIDragItem on the same sprite. Make sure the sprite has a collider and you should be able to drag it. (The camera in the scene requires a tk2dUICamera script on it)
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on December 15, 2013, 04:06:25 pm
1) Thanks but I still cannot get it to work.

Here is what I've done:

Created a quad, removed it's collider and added a Box Collider 2D to it, added Tk 2D UI camera to my main camera, added a UI Manager (from 2k td), added Tk 2D UI Item and Tk 2D UIDrag Item to my quad , in the UI Manager I selected my camera (that has the tk 2d UI camera script attached to it)

What am I missing?

BTW, I was testing with mouse, but I think it should work, right?

Thanks.

EDIT


I found the problem, apparently TK 2D doesn't work with 2D colliders and when I reverted back to Box Collider, it worked. Please do correct me if I'm wrong.

Thanks

2) is this a good idea to have this in my game when there are like more than 30 of draggable objects that move accross the screen at a time? I'm asking because it's named "UI" and I wonder if it has any considerable overhead that is not advisable to be treated as a regular draggable object for heavy lifting.

3) When my object gets touched, can I catch an event or message so I can handle some game logic with it? For example in my game these draggable objects are moving constantly and when you touch them, they have to stop and in order to detect that, I have to know when TK 2D detects a touch on them that I can catch as well.

3) Can I get the vector or direction that my obeject was getting dragged? In my game, when player draggs and releases the draggable object, I want my object to move along the direction that it was getting dragged, as like it's getting thrown in 2D.

Thanks again.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on December 15, 2013, 07:48:26 pm
You're right it only works with 3D colliders. 2D may be supported in the future, but there are no concrete plans to do so just yet.

2. Its perfectly fine - only one ray cast is performed for all colliders. Its much more efficient than some other approaches.
3. You can hook into the necessary events here: http://2dtoolkit.com/docs/2.3/ui/system.html
3. No, but you can attach a behaviour that sums up an average movement vector.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on December 18, 2013, 10:18:20 pm
Thanks, that did it.

Would you please help me with the number 3, the sum of movement behavior?
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on December 19, 2013, 12:00:07 am
Bascially you want to accumulate how much you've moved while you're clicking. Just keep note of the start position, then calculate the delta every frame and create an average with that. When you let go, use the average velocity and dampen it over time to have your sprite drag on for a bit...
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on January 28, 2014, 03:54:37 pm
Bascially you want to accumulate how much you've moved while you're clicking. Just keep note of the start position, then calculate the delta every frame and create an average with that. When you let go, use the average velocity and dampen it over time to have your sprite drag on for a bit...

Thanks.

Since I have to write code in order to work with 2D Toolkit, would you please tell me where I should write this code? i.e. which events should I listen to for when player drags and releases dragging the object.

I mean since it has to work with UIdrag, my script should be able to listen to those events, right?

Problem is, if player doesn't do a simple drag and release and moves the item a bit around, when she releases it, it would still launch based on first and last position and do not take the middle movements into account.

I checked the UIDrag code and I think you think of them as buttons, am I right? So I think I should copy and paste your UIDrag and do the accumulations and whatnot in the ButtonRelease event. I did that but the simple drag and drop doesn't work anymore!

My script is copy paste of your tk2dUIDragItem script and just renamed the class and file name so it wouldn't clash but in the editor I can see two new public fields UI Item and UI Manager that are both set to Null but your tk2dUIDragItem doesn't show these fields. Why is that and also why they don't get automatically get value as your tk2dUIDragItem does.

Another strange difference is that your script components in the editor do not expose the word "Script" in their name but whatever script I put on an GameObject, does! Why is that?
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on January 29, 2014, 12:00:17 pm
you need to copy the editor class as well - tk2dUIDragItemEditor.cs, duplicate and change appropriately for your class.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on February 12, 2014, 04:34:06 pm
you need to copy the editor class as well - tk2dUIDragItemEditor.cs, duplicate and change appropriately for your class.

I still cannot achieve this. Would you please help me further on this?

Thanks.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on February 12, 2014, 06:22:13 pm
What have you tried? You need to duplicate that class e.g. tk2dYourDragItemEditor, then change the references to tk2dUIDragItem in the c# source. It should just work once you've done this. It must be in an editor folder
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on February 15, 2014, 04:28:01 pm
OK, finally got it working with the duplocation of those 2 files.

1- Can I move my copies of tk2dUIDragItem and tk2dUIDragItemEditor somewhere out of the tk2d directory so it'll be prone to updates of tk2d?

2- I'm trying to get the average on drag and release but can't get it to work. I've tried to get the average of current position and last position in UpdateBtnPosition and pass it to rigidbody.addForce in ButtonRelease, but it doesn't work.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on February 15, 2014, 06:07:22 pm
1. You can leave them there, or move them anywhere else. I shouldn't make any difference. I recommend not using the tk2d prefix to avoid name collisions with future tk2d classes.
2. What doesn't work? What happens? Have you tried scaling up the velocities, etc? Have you tried rigid body.MovePosition? Check the unity docs for other options.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on February 15, 2014, 10:14:21 pm
1. You can leave them there, or move them anywhere else. I shouldn't make any difference. I recommend not using the tk2d prefix to avoid name collisions with future tk2d classes.
2. What doesn't work? What happens? Have you tried scaling up the velocities, etc? Have you tried rigid body.MovePosition? Check the unity docs for other options.

1- Should the editor class be in a folder called Editor or it can be anywhere?

2- What I've tried so far is to store last 100 position of the object while moving it around, then calculate average of those positions in order to get a vector3 that act as velocity vector of movement and then pass it to Rigidbody.addForce, but that doesn't seem to work.

Thanks for your replies.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on February 15, 2014, 10:40:57 pm
1. editor must be in an editor folder. Please check unity docs for more details on this.
2. Have you tried MovePosition to explicitly calculate the target position yourself and move it? You might have better luck with that.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on February 18, 2014, 01:07:09 am
Thanks.

I followed your advice and did this:

1- In ButtonDown() call of tk2dUIDragItem_idn (my copy of tk2dUIDragItem), I stored startPosition via newWorldPos or offset

2- In the ButtonRelease, I tried to minus the end position from the start position via:

vector3 direcionToLaunch = transform.position - startPosition;

and then call addForce with this direcionToLaunch. But it doesn't work.

What I suspect is that I'm not getting correct position from tk2d. I saw that you do some world to camera translations and stuff like that and made me wonder if this standard approach of reducing endPosition from startPosition to get the movement vector might not work with the position you calculate and store in transform.position of game object.

So if you would tell me which positions I should use when I'm trying to do these calculations, I think that would help.

Thanks.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: unikronsoftware on February 18, 2014, 10:29:49 am
transform.position is the world position of the object. How can that be wrong?
I have suggested trying MovePosition twice now, have you bothered trying that yet.
Title: Re: How to have a draggable object with 2D toolkit?
Post by: test84 on February 18, 2014, 07:55:34 pm
Thanks man, solved it with two different approaches and I post them here for future reference:

1- Storing start and end position of touch and then reduce the end from start and use that vector as force to rigidbody.

2- Storing last n positions of touch, then calculating vector between each two that are next to each other, then calculate average of those vectors and pass it to addForce.

I know most of this topic was not 2d toolkit related at all and I appreciate your helps very much.