A user posted a slightly inverted issue last august with no replies.
I’m using a 32 by 32 grid and ive got a:
Node2d
sprite2d
area2d
- collision shape 2d
Scene that is able to be dragged and dropped onto the grid after being instantiated.
The position of all of those nodes are set to 0,0 and the offset is not set to centered. The scale of all child nodes is also set to 1,1
Code:
Const TILE_SIZE: Vector2 = Vector2(32,32)
func _process():
if is_dragging_build_object == true:
build_object.global_position = get_mouse_global_position()
Then I have an unhandled input event left click set is_dragging_build object to false and
build_object.global_position = get_mouse_global_position.snapped(TILE_SIZE).
The dragging of the object works, the 0,0 top left corner of the object is aligned with the 0,0 tip of the cursor. However my issue is that the .snapped() properly snaps the object to tiles based on the center (in this case the 16,16 point) of the object. So which ever tile the center 16,16 point of the tile is in is the tile that the object snaps too. Not whatever tile the cursor 0,0 point is at or whatever tile the objects 0,0 point is at.
I’ve tried adding an offset like so:
.snapped(TILE_SIZE - TILE_SIZE/2.0)
and
.snapped(TILE_SIZE) - TILE_SIZE/2.0
and
14 other variations of such
The object still snaps to whatever tile the center of the object not its origin is in when the .snapped function is called.
Maybe I am misunderstanding the .snapped() function a little bit. Either way would somebody please help me get the .snapped() to snap to the tile that the 0,0 point of the get_global_mouse_position() is in or the 0,0 point of the object instead of wherever the center of the object is? Thank you!