- Home /
Multiple Clickable Areas On Sprite
I can't find a way to work with multiple 2D collision boxes on one game object.
In a card game, I have card objects whose sprites have various numbers on them. I need to know what number was clicked on. I have code to detect a click within a 2D collision box and print the gameObject tag, but have no idea how to distinguish one collision box from another to find the number clicked.
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.tag);}
123.png
(1.7 kB)
Comment
Answer by LCStark · Sep 18, 2018 at 08:13 AM
If you add the colliders as child objects of the Card
object, you will be able to give them separate names / tags:
Just remember that your hit.collider
GameObject will be the child object, not the Card
. You can still access the Card object by using the parent
property: hit.collider.transform.parent
.
colliders.png
(1.7 kB)