Drag a RigidBody3D and make it Snap to a position

Godot Version

Godot 4.2.1

Question

I have a 3D game in which a player must cut a brick to a different size in a minigame. As soon as the player interacts with the saw, the camera changes to a sideview so the game looks 2D. I want the player to now use the mouse to drag a brick [RigidBody3D (if you guys recommend another Bodytype, I am willing to change this, it’s not really that important to me what type it is at the end)] from left to right and I want the brick to snap into place.


the white rectangles sticking out of the brick are the places I want the brick to snap into at the white cylinder right below the saw.

So now I have 2 questions.

  1. How do I make the Brick draggable by mouse (I only want it to be moveable on the x-axis)

  2. How do I make the brick snap into place under the saw at the exact 3 locations shown in the picture.

    • I want the Brick to be still moveable after it has snapped, since obviously only one cutting position is the right answer depending on where the brick is needed. So once it locks into place, I want the player to be able to click on the Brick again to move it away if he isn’t happy with the snapping.

If you need any other type of information, feel free to ask.

Read about object picking here

Probably with an area3d where a brick can snap And maybe a raycast to select the area you are snapping to. Once mouse is released move body into position of area.

Godot has no concept of snapping so there is no need to worry about it being snapped permanently.

About the InputEvent part, now I know how to get the Input, but what would I need to code so the Object actually follows my mouse?

I thought of using the “get_global_mouse_position()” function but it says that this function is not found in base self. I was trying to make a script for the rigidbody itself. should I rather add the movement to the worldnode everything is a child of?

I would really appreciate it if you told me how the code is supposed to look aswell. Since I really have no idea what I would have to write to make the object follow the mouse because the idea I had with the global_mouse_position does not seem to be working.

Thank you for the snapping answer, I have now implemented the areas and coded something that should work. to test it I now have to code the movement still.

I would use mouse motion events to begin moving the object. Otherwise I think that function is on the viewport.

get_viewport().get_mouse_global_position()

I didn’t confirm this