NGUI OnDragOver() pass through when colliders overlap - c#

we are using NGUI, some collider has to be overlapped, and we want the ONDragOver() event received by the collider behind, ie should pass through to the underneath collider, Here is a demonstration of the situation:
The bigger one is on top of the smaller ones, and they are in the same hierarchy so that I can drag to move all of them.
I have tested that when object dragged over
void OnDragOver(GameObject that)
{
Debug.Log("you are on drag over workpanel not the brick slot");
//Debug.Log(" I am being draged over by : " + that.name);
}
This log will show to indict that the bigger collider will receive the event.
But actually, I want the smaller one in it to receive the event :-(

I have figured this out, change the z position of the collider want to hide behind to a larger value, so the one with a smaller z will get the event first, this may help with people with similar issue.
This may cause the bigger collider not to get the event when hit on the smaller collider, but, whatever, I think this will be good enough for this situation, if someone has a better solution, let us know ;D
And you also should know that the panel meters too. If you have a button collider that became not clickable after colliders overlapped, try to arrange the button in a new panel and tweak the panel depth.

Related

Unity drag and drop check right or left (2D)

I wanna build a card game in Unity.
I want to player swipe cards left or right but I wanna show some tricks about what's gonna happen if they swipe left or right so if they grab the card (2D Image) and go a bit left or right a text will appear on image depends on side.
I tried to use swipe and use X axis change but It's not working properly.
Here is a basic pictured information about what i wanna build:
I would advise using the drag handlers system in unity to track where the card started, and trigger different effects at different distances / directions from its origin.
You can detect the drag of the card by implementing IBeginDragHandler, IDragHandler, IEndDragHandler on a script on your card object.
Begin is called when the object starts dragging, Drag is called during each frame its moving, and End is called when the object is dropped.
When a drag begins, you can cache the position where the card started. Then in Drag handler each frame you can check the distance of your card from where it started. Once it has moved more than a certain distance, you can trigger whatever tricks or effects you want to show.
Depending on how your coordinate space is set up, you could for example know left from right during drag updates such as:
// Inside of your on drag method, check each frame for where the card is
// If it has moved far enough, trigger an effect
if (Vector3.Distance(dragStartPosition, transfom.position) > yourTriggerDistance) {
// if X is greater, the card moved right
if (transform.position.x > dragStartPosition.x) {
// Trigger Right swipe text
ShowSomeRightSwipeText();
}
// otherwise, X is smaller, so the card has moved left by the required distance
else {
// Trigger left swipe text
ShowSomeLeftSwipeText();
}
}
Also please remember that:
In order for these interfaces to work, you need to have a Physics 2D Raycaster attached to your camera in the scene.
You need to make sure you have an Event System in the scene as well.
The above two things are often asked issues here regarding detecting object dragging.

Unity2D Moving along grid lines

I'm making what is essentially a board game within Unity 2D. The player will be able to move their pieces along a grid (see below). The larger circular sprites are the pieces, which the player can click on, then click a gridPoint (the small squares) to move to it.
I've created the grid by using empty gameobjects (I've rendered a sprite on them for visual help) at each intersection. These are the points where players can move between. The distance between the points varies, though the horizontal distance is always 1.
The full functionality would be...
A player clicks on a piece,
A preview area of where the piece can move to is shown.
Player clicks on a valid grid point where the piece should move to.
Grid piece moves to new point.
First of all, have I created this in the right way? I need the grid to be accurate, as it will be populated with an accurate sky map. Currently this grid is accurate.
Assuming this is the right way of doing it, how do I, when a piece is selected, work out which grid points are within range? Say a piece can move 4 spaces, then any space 4 grid points away from origin (horizontally, vertically and diagonally) can be moved to. I'd also need to count how many spaces the piece has moved.
I also need to make the piece travel from it's original position, to the new grid point, via grid points using the shortest route.
Any help is appreciated.
OK, there are many solution for this, but let's keep it simple!!
simple overview of your solution step!!
If your grid is fixes size(i.e fix height and width unity length), then put custom placeholder (empty gameobject)in intersection point
detect the swipe detection(assuming you working with this mechanism) from current tapping piece to direction.
using any tween library(i suggest DoTween library from unity asset store, it's free and absolutely amazing.)move your piece to that placeholder.
Now bit more in depth Understanding!!
please refer the below reference.
initial condition of any type of grid
after creating the grid, you can put empty gameobject as an placeholder in between grid(you can make it invisible- i.e make it alpha 0 of image property!)
placeholder in between gap of grid
you can make you piece(player) child of any of these placeholder.
you will detect the swipe or even tap mechanism and check weather in what ever direction you swapping or in which invisible placeholder you tapping, has any child??
if yes, that means that placeholder is already occupied with other piece(player). so no logic to move your current piece. so ignore the swipe.
if no, that means placeholder is empty, so its valid move.
using any tween library, from current placeholder position to desired placeholder, move your piece(player).
after reaching to desired placeholder, make that piece the child of that desired placeholder(using setparent(desiredplaceholder.gameobject.transform.position) kinda code)
and you done!!
hope you find this helpful!!

Check if one GameObject is placed on top of another in Unity

I have two cubes with one being slightly bigger than the other and can be moved around freely around the scene by the user.
I need to know when the user has placed the smaller one on top of the bigger one and then continue with the script.
I understand that I need to check the collision somehow and maybe check the Y coordinate of both boxes too to make sure the user puts the smaller cube on top of the bigger and not vice versa.
How can I do this?
You could either create a trigger (more on that in the unity docs) and use the respecrive callback to continue or you could constantly check the position of the second object and if the coordinates are above the ones of the big box continue.

Detecting when a Picture Box touches another control

I made a game where the player controls a square, whenever it hits a wall it is supposed to die. The Square is a Picture Box and the walls are picture boxes too. You can move using W,A,S and D. I was thinking about doing something similar to:
if(Square.Top == Square.Top + Square.Height)
and then restart the game. Is there any better way doing it? Istead of having alot of IFs? Whenever a control touches another to do something?
Thanks alot !
Yes, you get the control's containing rectangle by Control.Bounds, and then use IntersectsWith with another rectangle.
if(Square.Bounds.IntersectsWith(Wall.Bounds))
{
// ...
}
Keep in mind that it won't trigger when your square touches a wall, only when it starts going on top of it, but I assume that's what you want.

How to limit cursor movement in WPF based app?

I'm working with a WPF app, more specifically a Canvas with draggable elements.
Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.
The event which can start a drag is shown below
private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
... logic to figure out if this is a valid drag, blah blah blah ...
this.IsDragging = true;
// TODO: clip the available cursor movement to actual width of Canvas
}
On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.
I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.
Anyone have a better way to limit the cursor boundaries?
There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.
Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.
If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.
Good luck!
You should be able to do it using the ClipCursor native API.

Categories