Get all objects overlapping a collider
Hi guys,
So I'm a bit new to Unity, but I do have experience in the Unreal Engine so I have an idea of what I want to do, I'm just a bit unsure of how to do it.
So I'm working on a 2D game and a melee enemy will have a melee trigger box in front of them facing towards the player and will swing to hit them. After a small delay (when the enemy actually hits the player visually) I want to check to see if the player is still within the trigger box.
In the Unreal Engine this would be as simple as check all overlapping, an do a for each loop to check if the overlapping collision is a valid target and damage that target if it is. I want to do something similar here, where I get all overlapping, do a foreach and check if the label is equal to a player, and if it is, deal damage to the player.
The closest thing I have managed to find is using the OnTriggerStay2D and creating an array of Contacts, but I have played around with this problem for a few hours and to be honest I don't quite know how the Contacts and GetContacts work and the Unity documentation I have read so far is inadequate for a beginner. Any help here would be great thanks :)
Answer by Tisreece · Apr 10, 2021 at 10:22 PM
In the end the way I did this was to create a list of overlapping colliders, and then when anything enters the collision it gets added to the list, and whenever it leaves it will be removed from the list.
Then, with the list, you can get a list of all currently overlapping colliders to check how many players/enemies are currently there to be damaged.