[SOLVED] Drag object towards latest/current touch position x

Hello, I want to drag an object towards the x position of the lastest/curent touch position in the current scene. I use the draggable behaviour and Physics 2.0. I do not prefer adding a force towards a position. How can I do this (conditions/actions)? Thanks in advance!

You want to drag it with the touch, or move it to the last touch position?
That’s not the same thing.

I want to move it to the last/current touch position without time delay, so the object is directly at the last/current touch position (x).

You can Lerp the object’s X and Y position to the Mousex() and MouseY() respectively. It’ll move the object smoothly (this link has a good explanation on lerp).

1 Like

Following the current cursor position is possible. You need to use a physics joint and push the values to their max to avoid delays.
Moving instantly to any position of the screen is teleportation, and that doesn’t fit with the laws of physics (yet, lol), so maybe you will have to disable the behavior, move the object to the desired position, and re-enable it.

Mixing several position behaviors is probably not a good idea, better avoid Draggable and use Physics with joints.

1 Like

Should I use the mouse joint then?

Yes, that sounds like a good idea. :slight_smile:

1 Like

Changing the positions is faster than add a force with the physics.

The solution from @MrMen is the faster solution, and give a immediate movement on screen.
And if you want a smooth movement between the starting point and where is touched the screen Lerp fonction will do the job Like explain also @MrMen.

Draggable is a behavior that can be easily reproduce with few events.
If Object sprite is touched, do the position of object = to MouseX() & MouseY()

If your object is a ball with physics think to disable the physic behavior when you move it or atleast clear the forces.

1 Like

Thanks for all the answers. I am not really experienced using gdevelop. I can’t disable physics 2, because there are forces working on the object (gravity etc). I only want to change the Ball X position, even if the ball is not touched (for example if you touch in the area below the ball, see image). Can you give me some concrete conditions/actions? Thanks for all the help. I really appreciate it!

When the touch is active you can get the touch x and y like with the mouse.
It’s not mandatory to hovering something, you can simply get the touch position.
Test a touch on an object is useful only if you want detect an action from your user like “He touch the screen”

So if you want move the ball on x-axis depending the touch on screen, you can add an action for change the x position of the ball, and in value use the Touch expressions.

You can open this example and see how works touch on mobile!

Here the wiki page for touch.