- Home /
Question by
darkjuplay_unity · Jun 23, 2019 at 01:55 PM ·
gameobjectraycastarray
Return a List of Gameobjects at the mouse position
I want to get a list of all the GameObjects that are at the mouse position and then try to get a specific gameobject by identifying it with the Tag or with its sorting layer. The first thing I wanted to use is a Ray but then I thought that using a RaycastHit2d only returns one gameObject (the first it hits) and I don't know an alternative. Does someone has a idea how to solve that? I would be very thankful for answers.
That's the code I wrote till yet:
RaycastHit2D rayhit2dph1 = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f); if (rayhit2dph1.collider != null)
{
//Testen ob es eine Figur oder ein Tile ist
SpriteRenderer spriterenderer2dcollidergameObject = rayhit2dph1.collider.gameObject.GetComponent<SpriteRenderer>();
if (spriterenderer2dcollidergameObject != null) {
if(spriterenderer2dcollidergameObject.sortingLayerName == "Figures")
{
} else if(spriterenderer2dcollidergameObject.sortingLayerName == "Tiles")
{
}
}
Comment