Godot Version
4.2.1
Question
I’m working on a card game using this GitHub drag-and-drop Godot 4 card tutorial but with just one hand of cards instead of two.
I’d like to add a camera2D to this scene and have the cards on the HUD in the foreground while moving the camera across a scene in the background (e.g. like a sidescrolling RPG with a hand of sortable cards that stays at the bottom of the screen). But when I add a camera2D it breaks the drag-and-drop functionality as it’s based on the global position of the mouse and the cards; unless the camera2D stays at 0,0 the cards will not drag-and-drop and instead become ‘stuck’ to the mouse or fail to drop between cards and return to their original position. I want to modify this project to work with a camera but I’m already over my head in terms of my understanding of C# Godot.
You can find the original script via GitHub here but I believe these two sections are what I need to revise, specifically the selectionOffset which is based on the global (not local) position of cards.
public async void CardIsClicked(InputEvent @event, Card card)
{
if (@event is InputEventMouseButton mouseEvent)
{
// Not only check for left mouse button pressed, but also only allow a single card to be selected.
// (In case of cards overlapping within a hand we get multiple events per click)
if (mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left && selectedCard == null)
{
selectedCard = card;
selectedCard.ZIndex = 1;
selectionOffset = mouseEvent.GlobalPosition - card.GlobalPosition;
card.tween?.Kill(); // Immediately grab card, do not let existing animation finish
}
public override void _PhysicsProcess(double delta)
{
DoRaycastQueries();
if (selectedCard != null)
{
// Without a correct offset, a card's center point will snap to the mouse, regardless of where on the card we clicked it
//This will reset position changes made below
selectedCard.GlobalPosition = GetGlobalMousePosition() - selectionOffset;
}
Can anyone help with modifying this project to work with a camera2D that moves (i.e. pans left and right)? I think this might involve calculating the local position instead of the global but I’m really struggling to find any C# specific Godot guidance on this. Thanks for reading!