Learning some Unity by starting out with a simple Solitaire game. I’m at the point now where I can drag and drop the cards around the board but not sure how to go about doing hit detection.
My basic setup
I have a GameManager object that on Start() will Instantiate 52 Card gameObjects, assign sprites, card values, and randomize them in an array. I.e. a shuffled deck of cards.
In my Card logic I’m hooked into the OnTriggerEnter2D, OnTriggerExit2D, OnMouseDown, OnMouseDrag, and OnMouseUp events. My Card gameObject also has some events of it’s own which the GameManager subscribes to, OnClick and OnDrop.
This allows the GameManager to contain all the game logic. I’m however not sure how to tell the GameManager that the currently selected Card was just dropped on to another card.
OnTriggerEnter2D fires for each object it collides with, in my case and entire stack of cards but I only need to know what it’s colliding with when the OnMouseUp event fires, which I don’t have access to the colliding object in this method.
I’m sure there’s something built in to tackle this problem but haven’t found a solution.
Also just a general tip, you probably don’t want to be creating a single monolithic class that handles all logic in your game. Splitting up the responsibilities into a system of objects can make your life a lot easier, and is the basis of object oriented programming, as well as component based design.
Just as an example, you could create a CardStack class, which has the stack of cards as children, and knows which card it can accept next. The Card class being dragged could detect the CardStack via trigger Enter/Exit, and maybe call a function to check if it can be accepted. This would allow you to potentially indicate to the player whether the drop will be successful or not, then OnMouseUp, call a function on the CardStack to accept it, which adds it to the stack, or reject it, which sends it back to the deck area.
This is awesome and thanks for the help. Sounds like OverlapCollider is what I need. This project is just to start learning Unity and understand the apis and concepts so I’m skipping a lot of basic design principles. I’ve been doing development for 25+yrs professionally so I’m comfortable with OOP and the like. I’m just making a mess until I can get the paradigm shift worked out in my head
I’m really surprised on how powerful (and fun) unity is to work with so far. Last time I tried any game dev was back when DirectX 3 was released.
There’s nothing wrong with polymorphism. When I say “the basis of object oriented programming” that covers encapsulation, abstraction, inheritance, and polymorphism.
But not all that is necessary for a small game like this. I’m specifically talking about making objects have dedicated responsibilities, containing any logic relevant to that object, and not depending on external stuff if possible. Just for organization purposes if anything. Keeping code modular makes creating class interactions a lot easier.
@jc1231 is describing a single class handling the interaction and game logic of all other objects, which is the opposite approach, all things point back to one controller.
However, since it’s such a small project and they are a beginner, either approach can reasonably work and is a good learning experience.
Ideally, a small project would make more sense to have a master script. However, I do see your point here. This isn’t the best strategy to use whilst learning code because it does require a well thought out structure and methodical planning.
I finally got around to trying this out and it seems to only detect some of my objects and not others. I have 2 types of prefabs. Cards and CardSpaces. Both have Box Collider 2D components attached and the selected, moving, card has a RigidBody2D. When I MouseUp on a stacks of cards stacked on top of a CardSpace, only the CardSpace is being returned by the OverlapCollider. I’m using an empty contact filter new ContactFilter2D() not sure if this is why.
It’s impossible to say without looking at the project. If you know for sure there are colliders overlapping then it can only be that you’re filtering them out. To get a ContactFilter2D that does no filtering you should use: Unity - Scripting API: ContactFilter2D.NoFilter
If you’re using the overload that allows you to pass an array then you need to ensure your array has more than a single element. You’re best to (re)use the overload that accepts a List.
Due to C# not allowing a struct to have defaults other than the default for the field types a new ContactFilter2D will allow everything through apart from triggers. Non including triggers was a reasonable default for most queries so typically it’s not an issue. In hindsight, it might’ve been better to also include triggers but it would then mean a majority would have to explicit set to not use triggers so it’s not a win either way.