Collisions between UI elements in Unity - unity3d

I am able to detect collision between UI components and a gameobject if my canvas is rendered in the world space. Here, I am trying to find collision between two UI elements (say UI buttons) when the canvas render mode is screen space overlay.
I added box collider components to my UI buttons and tried using OnCollisionEnter2D and OnTriggerEnter2D. But, the collision is not detected. Is there a way to detect the collision?

The question does not require a code body. However, I have figured out a solution. TO both the UI elements, you need to:
Attach a rigidbody2d component
Attach a box collider component
disable gravity
enable the isTrigger checkbox.
Now in the script attached to one of the UI elements:
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log ("Triggered");
}
This would detect the collision.

Related

Is there a way for a gameobject's collider to only respond to a pinch gesture?

I have a collider that when grabbed (pinch gesture), it runs a method. However, I don't want the collider react to select events. Right now, the collider prevents buttons behind it being pressed. So when the pointer is coming from the hand, the ray stops at the collider and doesn't reach to the button behind it.
From my understanding, if there is a collider, the hand interaction ray stops at it, no matter what components it has. My guess is that I need to have a layer that ignores these colliders that still allows for a grab event to be registered. But when I added a layer for the Pointing Raycast Layer Mask, it correctly ignores the collider for the far interactions, but fails to recognize a grab event.
This is how my interactable component looks:Interactable Component
I appreciate any help! Thank you
In the input settings, change the size of Pointing Raycast Layer Masks. Set UI to be element 0 so that pointing events react to that first. documentation

OnMouseDown event received outside camera view port, where object is not visible

An object has a collider 2D and is outside the camera viewport. Below the camera viewport to be exact.
The camera is rendering only on a portion of the screen-buffer/viewport-rect. The camera is orthographic.
The object has monobehaviour attached with OnMouseDown event declared in it.
Green is collider, white is camera viewport.
This is game view. pink is area being rendered by camera, it is taking touch event on highlighted red area.
When clicked just below the render area of the camera, the object receives OnMouseDown. which it should not because the object is not visible.
I have tried reproducing the issue on Editor with no success.
There is only one camera in the scene.
void OnMouseDown()
{
Debug.Log("Clicked");
}
It behaves correctly on Editor, means it does not take a touch in Editor. But on Windows player (build) when touched below the drawing area (viewport rect) of the camera, the button receives the event.
Why the behaviour difference between editor and player?
It should not take touch on objects outside the viewport.
Upgrading to unity version 2017.4.26 fixed the issue. it is 100% reproducible in version Unity 2017.4.5.
It is an engine issue.
Possible solutions are:
1) upgrade to unity 2017.4.26f
2) as #derHugo said in above comment "Maybe you could use OnBecameVisible and OnBecameInvisible to simply enable and disable the collider?"

unity2d button collider collide with border when character moves

I made a button that is clickable during the game playing. As I want the button have a fixed position in the main camera, I made both the main camera and the button children of the player gameObject so that the camera will follow the character while jumping or moving, everything works fine but there also a border colliders which will prevent the character moving out of the playing area. But then the collider of the button which was made intend to make the button clickable will also collide with the border which will prevent the character moving right forward. If we set the collider of the button a trigger, it seems that the button will be triggered wherever I click the mouse on the screen, that's not what I wanted.
I know maybe I could prevent this by checking if the collided object is the button or the character, but is there a better way to do that? Thanks.
For a 2D platform game, I would add a 2D user interface in a canvas over the "map" (the layer where you have the gameobjects like the character, platforms, enemies...). So the button will be always in the same place of the screen and will never collide with any gameobject of the game.
You make take some ideas from here: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-events-and-event-triggers
Try use new UI in Unity may it fix your issue:
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

Touch not detectable past Panel - Unity3D/Vuforia

I have been trying to add touch event to 3D gameObject in Unity.
Previously I was not using any Canvas or Panel so by using Event Trigger and Event System I was able to add touch events to the Gameobject but then I wanted to use a UI for Application and implemented this hierarchy.
Camera
GameOBject
Canvas
Panel(Transparent)
-Buttons
Panel2
EventSystem
So if i tap a part it does not respond.
Camera does have Physics Raycaster.
Gameobjects have colliders and mesh renderers.
I want a touch input from the mobile device.
Thanks
If two Button overlap each other then the order in which camera renders top will be detected.
So try changing the hierarchy order (remember the last in the hierarchy is always top of the camera ) you can achieve which one to click

Unity3d with vuforia showing 2d image when targed is detected

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d.
The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is
detected. The original image on editor is placed then so far that it's difficult to find it.
I would be grateful if somebody can highlight me the right direction.
I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.
To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.
These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs
If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.
However, in case you want to do something more, here's some edited code.
DefaultTrackableEventHandler.cs
//Assign this in the inspector. This is the GameObject that
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;
Add the below lines to OnTrackingFound
//Enable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Found. Note that you can change the type of
//collider to be more specific (such as BoxCollider2D)
spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
spriteGameObject.GetComponent<Collider2D>().enabled = true;
//EDIT 1
//Have the sprite inherit the position and rotation of the Image
spriteGameObject.transform.position = transform.position;
spriteGameObject.transform.rotation = transform.rotation;
And the below to OnTrackingLost
//Disable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Lost.
spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
spriteGameObject.GetComponent<Collider2D>().enabled = false;
Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.
Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton
Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.
public class HandleClicks : MonoBehaviour {
//Event fired when a collider receives a mouse down
//and mouse up event, like the interaction with a button
void OnMouseUpAsButton () {
//Do whatever you want to
Application.LoadLevel("myOtherLevel");
}
}