- Home /
Actions in a 2D Card Game
Hi! Im trying to make a solitaire game using Unity 3D.
My problem is I don't know how to simulate the drag action of the card and when I release the mouse button it's going to snap on a another card when it's valid.
Is there a tutorial on how I can drag and drop the cards? Should I use a plane? Should I make them rigid bodies? And with the "drop" action how can I snap it so that the arrangement of the cards is still arranged?
It's not a trivial job in Unity - a normal 2D engine would be to 'drag' objects easily. But in 3d you have to deal with how far away they are, which makes it much, much tougher.
Off the top of my head, I'd say you're going to need to create an empty GameObject with a BoxCollider for each 'stack' of cards.
You will then need to trace a ray from your mouse position (using Raycast and Input) through to the cards to detect if your mouse is over one.
Then if the user presses the mouse button (Input.Get$$anonymous$$ouseDown(0)) while the cursor is over a card, you can then 'drag' it by simply changing it's position (translate.position) according to mouse movements.
Once your mouse (or your card) collides with the BoxCollider specifiying a row, you can change it's position to match the position of the BoxCollider. That way it will 'snap' to each of the rows. The position of the card would be equal to the position of the boxcollider, plus an offset for the number of cards in the row (so you can see the cards under the new one) and a very small 'bump' on the z-axis to make sure you don't get texture flickering caused by two planes in exactly the same place.
Answer by coastwise · Jun 01, 2012 at 02:53 PM
I was able to achieve card-like drag and drop with very little effort. I gave each card a RigidBody (with gravity turned off) and got drag-and-drop for free by using the Standard Assets DragRigidBody script.
To get the 'snap to specific position' effect, I used some basic raycasting and iTween.
The combined effect can be seen in this video.
Your answer
Follow this Question
Related Questions
2D poker Chip Animations (Similar to Zynga) 1 Answer
A little hand with a Card Game 0 Answers
Get child name from another parent 2 Answers
Making a card 1 Answer