Drag and Drop Fix Problem In Game (SOLVED)

Hi there. I make drag and drop in my game from this topic (Create drag and drop games? (SOLVED)).
But I can’t understand one thing. After press in Game Obgect, my object, when dragged, first jumps to the center of the click and then moves. How do I prevent it from jumping to the center of the cursor and immediately starting to move???
How I can fix this problem?
Below I will attach my video about this problem)))
https://forum.defold.com/uploads/default/original/2X/d/d1787d3c853e57468fe72a030d02e7c10e5a25d6.mp4

The origin of the sprite is at its center. So when you set its position (to the mouse pos in this case), you are setting where its center is.

Instead of just setting the dragged object’s position to (action.x, action.y), you need to offset it. When the drag action begins—when the mouse button/touch is pressed—get the offset between the mouse world position and the center of the object (object pos - mouse pos). Save that offset in a variable somewhere so it will persist over multiple frames. Then, when the mouse moves, set the position of the dragged object to (action.x, action.y) + the offset.

(Use proper vector3s of course, I simplified things.)

3 Likes

how I can do that in Defold. Script example please)))

Maybe something like this?

function on_input(self, action_id, action)
    if action_id == hash("touch") and not action.pressed then
        local p = go.get_position()
        p.x = p.x + action.screen_dx
        p.y = p.y + action.screen_dy
        go.set_position(p)
    end
end
8 Likes

That’s…a much better method than mine… :expressionless: I need to get more sleep. :man_facepalming: :smile:

3 Likes

Thank You so much)))

1 Like

thanks for all who was try to help me)))

1 Like