What is the most simple way to have get_mouse_position in a 3D space?

Godot Version 4.6

I’ve been trying to find a simple way to drag, reorder and move cards according to the mouse position similarly to balatro and a lot of other card games but as easy as it seemed in 2D, 3D has no easy way of pulling this off from what I’ve seen. I looked at addons like the Card3D addon where it did have a drag logic but when I add the necessary components listed it didn’t seem to do anything. I’ve seen youtube tutorials and other niche posts about this topic but I don’t want to just copy and paste someones code and pretend like I know what I’m doing. Is there a more simple way for me to both implement and learn this? I’m very much a beginner so all advice is very much appreciated. thank you.

I like to use raycasts to select 3D objects by mouse, you can make use of Camera3D’s project_ray_normal and project_ray_origin to have the raycast follow the mouse.

Cards sound like a 2D object though, could you explain your situation more? Why are your cards in 3D?

1 Like

It’s my second time being asked that on here actually! the game itself has 3D scenes and 2D scenes, for my battle system I’m using a 3D scene because although it’s mostly 2.5D I have a mesh in the scene that’s essential as it’s the game board. you could look at the way inscryption by daniel mullins’ implemented this if you’re unfamiliar with it. so while dragging and whatnot only getting the X and Y axis would be enough in this specific scenario for me. that’s basically why I’m trying to figure out how I’m supposed to implement this in 3D.

Ah, I see; Area3D does have signals for input_event and mouse_entered/mouse_exited if you aren’t dealing with physics bodies, that may be easier than calculating ray casts

1 Like

I use mouse_entered/exited for handling hovering indications currently and input.is_action_just_pressed() to handle selecting cards, I’m quite inexperienced so if it isn’t too much of a hassle could you elaborate a little bit more on how to actually set up a dragging logic? I mean I know that I should just add a process function and write something like if clicked: self.position = mousepos.position but I’m trying to figure out how to get the x and y positions of the cursor in the first place. again thank you dearly for your time​:growing_heart:

You need to establish an imaginary 3D plane where all the movement/dragging is happening, then intersect mouse ray with that plane to get 3D positions.

Well the X and Y position may be the easy part (get_viewport().get_mouse_position()), it’s the Z that you have to make a creative decision. Do you keep the card at it’s same Z position? move it along the view plane? Raycast to the nearest collidable object? Snap to your droppable zones? Or a mix of any and all of these options.

1 Like

when I hover and select the card its Z position is increased by a little to make sure it’s the card that’s on top of the other cards in hand, but besides that while dragging a card changing the Z position isnt needed at all! as long as it can move in the same X and Y position as my cursor that’s practically all I need!

1 Like