0
\$\begingroup\$

I have a 2d tilemap collider combined with a 2d composite collider to and set to be a trigger. What I want to do is check if there are any game objects within that trigger area. The catch however is that not all of these game objects have colliders. I cannot use collider bounds either since the trigger area can be wrapped around game objects without them being in it (see picture). At this point I have a list of game objects (got them by tag name), and I'm looping through them to check if they are in the room.

For more context, this is what is happening from the player's perspective: You're entering a procedural generated dungeon, the scene loads, and all the game objects are loaded with the scene (chests, stairs, points of interest, etc...). The dungeon is divided into sections, and as the player walks around and enters sections of the map (entering the trigger), that calls the method to find all the gameobjects of a certain tag name and check if they are in the trigger area so they can be shown on the map when the player enters that area.

    public void ShowMapIconsInExposedRoom(Collider2D roomCollider)
    {
        foreach(GameObject go in MapIconGameObjects)
        {
            if (/*game object is in the room collider*/)
            {
                go.SetActive(true);              
            }
        }
    }

TriggerArea

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Is Collider2D.OverlapPoint enough? \$\endgroup\$
    – Mangata
    Jul 5, 2022 at 17:36
  • 3
    \$\begingroup\$ It sounds to me like this would be better to solve during the generation phase. As you build your trigger areas and populate them with objects, save each object you create in a list associated with the zone you placed it in. Then later, you don't need to search for objects by tag and confirm whether they're in the right zone, you can just consult your pre-made lists directly. We may be able to help you with that if you show your generation code. \$\endgroup\$
    – DMGregory
    Jul 5, 2022 at 18:30
  • 1
    \$\begingroup\$ In addition to the other comments, it may also help to subdivide your trigger area into smaller, convex regions as convex regions are often easier to reason about with respect to things like as if point P is in region R, etc. \$\endgroup\$
    – Pikalek
    Jul 5, 2022 at 18:43
  • \$\begingroup\$ Points to Mangata! That was part of the solution, but it looks like the the composite collider geometry type needed to be set to polygon as well which was stumping me. It defaults to outline, I'm still kind of new to game development. DMGregory, I agree with you 100%. That thought did cross my mind (afterwards of course), and if I ever end up doing this again I'll go that route. However since this works and doesn't see to be too sub-optimal. I'll keep it going unless this becomes a larger problem. Thanks everyone! \$\endgroup\$
    – Vandel212
    Jul 5, 2022 at 19:44
  • \$\begingroup\$ @Vandel212 Please consider writing up your solution & posting it as an answer. Everybody is new to game dev at the beginning of their journey; leaving some marks on the trail may help others who follow after you. \$\endgroup\$
    – Pikalek
    Jul 6, 2022 at 1:19

0

You must log in to answer this question.

Browse other questions tagged .