- Home /
Detect 2d items only on areas where the sprite covers, and not the whole box!
Hello everyone,
I am developing a drag and drop game where shapes are not perfect squares, such as triangles ...
Objects are over lapping each other in the pool of object, when the front object is huge, the drag reacts on the full object square frame, where the area is transparent .Please check attached image.
check the cursor location, when I drag from cursor point, the pink triangle is being dragged, but the user tries to drag the purple one.
I don't think I have to arrange the triangles not to over lap each other.
What other suggestion would be ? thanks, Vasken
I've never used Unity for 2D, but i think the key is to have a collider that matches the shape of the sprite.
https://docs.unity3d.com/$$anonymous$$anual/class-PolygonCollider2D.html
The collider can be edited manually but it is often more convenient to let Unity deter$$anonymous$$e the shape automatically. You can do this by dragging a sprite asset from the Project view onto the Polygon Collider 2D component in the inspector.
@mj321 thanks for your answer, but as you see in the picture, the mouse doesn't even touch the collider. I know I can edit the collider but the dragging is related to the eventSystem.
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
here's the code.. so maybe if there's a method to let the event system drag by object only when collider is hit
In 3D i'd use a Physics.Raycast to find the object under the mouse and i know it would work. But i don't know anything about the 2D system. Sorry.