Unity 2D drag and drop without using Canvas - c#

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

Related

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.

Set limit camera screen WITDH

Look my game has a fixed camera, configured as a standard 1280x720.
I placed at each end of the cam width of an empty object with a collider to prevent the player pass this point.
Everything works perfectly until the default resolution decreases. When I run the test with 480x320 screen width decreases, but the collider, does not follow this decrease, staying out of the screen, which makes the player to be "cut off."
The following two images:
First: 1280x720
Second: 480x320
There is some way which I can only set the side margins left and right of the camera as a collider?
If it is not possible to configure only the edge of the camera as a delimiter, the player would have some other way to do something to solve my problem?
Some items fall from the sky, so I can not have a collider at the top of the camera.
There are many ways to do this. The easiest way is to create a 4 box collider 2D and position them to the edge of the screen.
I always use this complete script to do this. It's too long to post here. Just attach it to an empty GameObject in the scene. It will do the rest of the work for you. I will create the colliders and position them to the edge of the scrren automatically.
As what i have understand my be this can help you
Scale box collider to camera view

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

How do I make so when user moves the camera it doesn't go beyond the scene borders?

How do I make so when I move the camera(with touch) it doesn't go beyond the scene borders?
How do I move the camera with touch so it moves strictly with scene parts, like slides(swipe-first slide, another swipe-another slide) with not going beyond the borders of the scene?
The game I'm making has a camera like in Disco Zoo game for android (I'm a newbie)
Technically, the scene doesn't really have a border. You'll need to define the border somehow within your game, then constrain the camera's position with something like Mathf.Clamp(value, min, max) in an Update() function on the camera.
How can you define the border? It's up to you. Some ideas:
Hard-code the values in the script that clamps the camera. Probably the quickest option, but not flexible
Make public parameters on the camera script that let you set min and max positions in the X and Y directions
If you have a background image: use the extents of that to define your camera's extents
Create empty objects in your scene that define the minimum and maximum extents of the scene. Put your "min" object at the top-left, and the "max" object at the top-right. Connect it to the camera script, then use those positions to see if you've gone too far in any given direction. The main reason to do this is that it's visual.
(Slower, but dynamic) If everything in your scene uses physics, you could search the entire scene for every Collider component, then find the furthest extents in each direction. However, this is probably going to be pretty slow (so you'll only want to do it once), it'll take a while to code, and you'll probably want to tweak the boundaries by hand anyway.

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