- Home /
Click through Transparent Parts of Image
I'm trying to use images to build a board of irregularly shaped portions, however the bounding box for some of the irregular shaped parts overlap and it detects the click on the top element instead of the element that has the graphic below this. Is there some way to apply a selection mask or something to limit what regions of the graphic can detect a click?
Answer by brightskull · Dec 09, 2014 at 07:19 PM
Hi there, I might use recasting for something like this so that only the element that you click on will be affected.
Example:
public float sensitivity = 1.0f;
private GameObject gameObjectToMove;
void LateUpdate ()
{
// Click and Drag Mechanic
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if(Input.GetMouseButtonDown (0))
{
RaycastHit2D hit = Physics2D.Raycast(pos, transform.position);
if (hit.collider != null && hit.collider.gameObject.layer == LayerMask.NameToLayer("WhateverLayerYouWant"))
{
gameObjectToMove = hit.collider.gameObject;
}
}
if(Input.GetMouseButton (0) && gameObjectToMove != null)
{
gameObjectToMove.transform.position = new Vector3(pos.x, pos.y, 0);
}
if(Input.GetMouseButtonUp (0) && gameObjectToMove != null)
{
gameObjectToMove = null;
}
}
Would this detect the top element in the hierarchy? $$anonymous$$y problem is that collision detection is being put onto the lower image element and not clicking through the transparent part of an image.
For example, I have a circular piece inside the square image component for the bounding box. The corners which I don't want to be part of this piece still detect mouse clicks.
Answer by tanoshimi · Dec 09, 2014 at 07:24 PM
Clicks are detected by colliders. So if you want a more complex hit-detection, you'll need to add a mesh collider (or create a composite collider) that excludes those areas on which you don't want to be able to click.
Would this need to be paired with the On$$anonymous$$ouseDown function for a $$anonymous$$onoBehavior? It doesn't appear to work with 4.6 UI PointerClick event.
Your answer
![](https://koobas.hobune.stream/wayback/20220613173636im_/https://answers.unity.com/themes/thub/images/avi.jpg)