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

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?

Related

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.

XNA/MonoGame Mouse Over Cards

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

Unity2D - Method to create a fixed size dynamic terrain

I'm new to Unity/C# and building a simple game in order to learn it; essentially it involves flying over a terrain and shooting at things.
The question is - How can I create a non-flat, fixed size (ie, not endless) 2D terrain that I can randomise/generate once at the start of a scene.
Here's what I've thought/tried so far:
I started off by creating a massive, flat expanse using a game object with a Sprite Renderer and a Box Collider 2D. I'm using particles for rocket trails and explosions and set them to collide with a plane which worked well. This works, but the terrain is just flat and boring.
Next, I drew some rudimentary mountains in photoshop, added them as sprites with polygon collider 2D components and dragged them onto the scene (with an idea to randomise their locations later). This is quick and works well but doesn't play nice with the particles because of the 2D/3D mix, and I imagine would lead to all levels looking the same unless I can draw a LOT of different terrain sprites. Is this a normal approach? Is it possible to add more planes to the objects to allow particles to collide?
Another thought was - could I create a polygon collider 2D / or edge collider 2D and basically fill the area with a texture? No luck on that one, it seems like it could work but I couldn't get a texture to appear and fit the shape. A shame, especially as I could then generate or randomise the position of the points later.
Finally, I wondered whether I should just use 3D shapes. I can texture them and it works great with the particles, but doesn't interact right now with all my other 2D assets, I'd have to add 3D colliders to them and that doesn't seem right, since Unity specifically provides a 2D toolset and I am trying to make a 2D game.
All of my solutions have pros and cons - maybe there's something I've missed?
You can create meshes procedurally in unity in scripts. They can be either 2d or 3d depending on how you set the vertices. Colliders are independent of the rendered mesh. I dont have an indepth tutorial for this but i think this video will hint you in the right direction and give you keywords to google or read about in the unity documentation:
https://www.youtube.com/watch?v=nPX8dw283X4

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.

How To Produce A 2D Plane Cut from a 3D Image

I would like to write a C# program that generates a 2D image from a rendered 3D object(s) by "slicing" the 3D object or through a cut-plane. The desired output of the 2D image should be data that can be displayed using a CAD. For example:
A 3D image is defined by its vertices, these vertices is contained within Point3DList(). A method is then called taking Point3DList as its parameter e.g: Cut2D(Point3DList).The method then generates the 2D vertices and saved it inside Point2DList() and these vertices can be read through a CAD program which display it in 2D form.
My question therefore is whether there is a previous implementation of this in C#(.NET compatible) or is there any suggestion on third-party components/algorithms to solve this problem.
Thanks in advance.
You pose an interesting question, in part, by not including a full definition of a 3D shape. You need to specify either the vertices and edges, or an algorithm to obtain the edges from the vertex list. Since an algorithm to obtain the edges from the vertex list devolves into specifying the vertices and edges, I will only cover that case here. My description also works best when the vertices and edges are transformed into a list of flat polygons. To break a vertex list down into polygons, you have to find cycles in the undirected graph that is created by the vertices and edges. For a triangular polygon with vertices A, B, and C you will end up with edges AB, BC, and AC.
The easiest algorithm that I can think of is:
Transform all points so that your 2D plane where the Z axis is 0. (rotate, twist, and move as required to transform the desired 2D plane to line up with the XY plane where Z=0).
For each flat polygon:
a. For each edge, check to see if the vertices have opposite sign on the Z axis (or if one is 0). If Z0 * Z1 <= 0 then this is the case
b. Use the definition of a line and solve for the point where Z=0. This will give you the X,Y of the intersection.
c. You now have a dot, line, or polygon that represents the intersection of your the original flat polygon from step 1 intersecting the 2D plane.
d. Fill in the polygon formed by the shapes (if desired). If your 2D rendering package will not create a polygon from the list of vertices,you need to start rendering pixels using scanlines.
Each of the individual algorithms should be in "Algorithms in C" or similar.
Graphics programs can be quite rewarding when they start to work.
Have Fun,
Jacob
This is more opengl specific rather than c# specific, but what i'd do:
Rotate and transform by a 3d matrix, so that the 'slice' you want is 1 metre in 'front' of the camera.
Then set the near and far horizon limits to 1m and 1.001m, respectively.
-update- Are you even using opengl? If not, you could perform your matrix arithmetic yourself somehow.
It sounds like you want to get the 2D representation of the points of intersection of a plane with a three-dimensional surface or object. While I don't know the algorithm to produce such a thing off hand (I have done very little with 3D modeling applications), I think that is what you are asking about.
I encountered such an algorithm a number of years ago in either a Graphics Gems or GPU Gems or similar book. I could not find anything through a few Bing searches, but hopefully this will give you some ideas.
if its a 3d texture cant you just specify 3d tex coords (into the texture) for each vertex of a quad? wouldnt that auto-interpolate the texels?
If you are looking for a 3rd party implementation, maybe you should explore Coin3d. Capable of such things as you require, though I am not sure of its exact database format or input requirements. I find your description lacking in that you do not specify the direction from which you want to project the 3d image on to a 2d plane.

Categories