Move Node2Ds along same angle and distance

Godot Version

4.3 stable mono

Question

I’m doing a click-and-drag UI. I’ve got the right input handlers to track when I’m click/dragging a node, and _process logic to position it to the mouse global position. That works.

The issue I’m having is that in the game, you can click and select multiple nodes (dice), and drag them as a single group.

I can’t quite figure out the best way to do this. I can’t position them all to the mouse global position, since then they’ll just all clump on top of each other.

I thought maybe I could use the angle_to and distance_to between the die’s current position, and the mouse’s current position, and move all the nodes along that angle for that distance.

If so, how could I do that? I can’t quite wrap my head around the needed vector math.

Or am I overcomplicating this and there’s a better way?

You can do that way too, but is much more easier use an offset value:

Create a variable to store the offset value
var offset := Vector2.ZERO

Before start the dragging, set the offset value
offset = global_position - get_global_mouse_position()

And while you’re dragging, set the global position for the mouse pos + offset
global_position = get_global_mouse_position() + offset

2 Likes

Ah, that works. I’m tracking the offset per dice, and they move as a group now.

Thanks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.