Hello Guest

Author Topic: How to have a draggable object with 2D toolkit?  (Read 15242 times)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #15 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.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #16 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #17 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.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #18 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #19 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.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #20 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.