Distance float keeps flipping between positive and negative in _process

Godot Version

4.6.2

Question

So I started following this card game tutorial for Godot https://www.youtube.com/watch?v=1mM73u1tvpU&list=PLNWIwxsLZ-LMYzxHlVb7v5Xo5KaUV7Tq1&index=2

In the tutorial he uses this code in _process() to get the clicked on card to follow the mouse position

I noticed that with this code, it causes the card to immediately snap to position so that the center of the card aligns with the mouse position.

I thought it would be cool to try and edit the code so that the card moves relative to where it was clicked. I figured I could to do this by getting the distance between the center of the card and the mouse position and adjusting the cards position accordingly. This is how I edited the code to achieve this.

Now, it almost sort of worked. Except, when you click on a card, now, it starts flipping across the mouse position super fast.

2026-05-06 19-22-03

I added a line print(distance_x) to see what it was returning, and turns out, the value does in fact flip between the positive and the negative on each tick.

Things is, I can’t see a reason in my code that this would be happening. Thought maybe it was a weird interaction with the clamp function, but I tried removing it and it still did the same thing. Wondering if anyone hear has some idea why this is happening.

you need to store the “original mouse offset when drag started” instead of re-evaluating it each frame. on frame 2, the card center is no longer corresponding to your card_being_dragged.position (since you moved it on frame 1 to match the mouse offset)

3 Likes

That worked! Thank you!

2026-05-07 03-20-52

3 Likes