Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by carlflor · Apr 18, 2012 at 02:44 AM · 2dcardcardspoker

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgnoranceIsBliss · Apr 18, 2012 at 03:46 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jun 01, 2012 at 02:55 PM 0
Share

Nice video :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D poker Chip Animations (Similar to Zynga) 1 Answer

A little hand with a Card Game 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Get child name from another parent 2 Answers

Making a card 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges