XNA/MonoGame Mouse Over Cards - c#

Blue overlaps Green which overlaps Red.
Each card can be selected by passing the mouse over it. But the thing my hitboxes don't have a depth notion (z-axis), it's a 2D game.
So lets say that i want to select the Green Card when i put my mouse over it the Green and the Red are selected because the cursor is in the Green HitBox but also in the Red HitBox.
So my question is how should i manage this: When i have overlapping hitboxes, how to check only the area that are not covered ?
Note : I use the Rectangle Intersect and Contains functions.

But the thing my hitboxes don't have a depth notion (z-axis), it's a 2D game....So my question is how should i manage this
Just because it's a 2D game (and by that I mean the camera is projecting some world from xD to 2D) doesn't mean your scene has to be in 2D. Because your cards can overlap one another your scene has depth and so it is 3D.
Once you realise this, hit detection of objects in your 3D scene are trivial.
i.e.
shoot a ray from the mouse, inverse projected into the scene
test to see which objects it hits
take the first object closest to the origin

Related

Unity 2D drag and drop without using Canvas

I have a board that made with bunch of square tiles, i have player which is circle and i wanna drop over the tiles and set plater postion to the center of nearest tile while mouse button up ;
is there any way to handle this without Canvas and EventSystem ?
There are a few ways to do it,
You can store all the tiles in an array and when dragging the player check for the position of the closest one, this can work great if you have gaps between tiles and want the player to still snap on to the tile even if the player is not over it by having a threshold setup, it could get a bit costly though if you have thousands of tiles and you are iterating through them all.
Second and the easiest one is to just cast a Raycast from your player straight to the plane where your tiles are positioned, on hit just position the player in the middle of the tile. If you have more objects in the scene you can also tag the tiles with for example "Tile" and in the raycast check for the tag. The use of raycasts is documented here https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Unity - Mapping input on a 2D Plane to a 3D Object

So I've got an idea and I'm wondering if it's even possible, and if so, how to implement it. Say I have a 3D Globe in a scene as a representation of Earth, and in the corner of the same scene I have a 2D map of Earth. I want to click on an area on the 2D Map, say Japan, and have a gameobject appear both on the 2D map and at the correct spherical coordinates for Japan on the 3D globe. Vice versa implementation would be great too. Is that something that can be done?

Projecting rectangle on Sphere in Unity

For a VR project I have a 360 panorama on a sphere in which users need to mark something by "drawing" a rectangle with their head movement. So let's say you'd want to mark the person in the image below you start drawing at for example the top left corner like so.
Then move your head to the bottom right and end up with a rectangle, something like this.
How would i go about doing this? I guess i would somehow have to project a plane on the panorama sphere based on the camera position? Any help would be appreciated!
You need to determine the gesture/action for starting to draw and an indicator for each corner of the rect. (you are basically creating a mesh/quad at runtime, herewith is a tutorial to help you with that https://www.youtube.com/watch?v=gmuHI_wsOgI )
Now you need to find the wanted vertex positions for the quad. I would try to raycast and get the hit.point values. collect 4 raycast points and assign their values to the mesh vertices.

2D Colliders collisions

I have a problem with physics. I'm trying to do something and I do not know if this is possible in Unity2D.
In the picture you see what I want to do. I want that circle to get into that cone.
The problem is that if I put in a collider circle it collides with the cone. That's why I wonder if there is any way to do this without 3D elements.

How to make 2 GameObjects drag proportionally?

I have 2 GameObjects, the first one is on a 2D interface (plane), and the second one is a 3D character (also sitting on a plane), so the 2D texture represents the 3D character in the game,
I want to drag the 2D texture and have the 3D character move with it, but I want it to be proportional, for example if the 2D texture reached half of its plane the 3D character must be as well at same position accordingly, and the 2 planes are not equal as well (width-height), for example 2D plane is 2x3 and 3D plane is 9x5,
Can anyone please help doing this ?
PS: I am using NGUI to draw my 2D interface
I think i understand your question.
You might want to make a class that scales all the movements.
(I do this when making GUI's so that screen size doesnt matter)
XofPlayer = (Xof2d/widthof2d)*widthof3d
Does this make sense? IF you have the size of the 2d plane and an object get to half way on that plane. The player will then also be halfway on their plane. No matter what the two sizes are. You would do the same thing with the Z axis for depth. And I guess Y if you need it.

Categories