- Home /
Populate an array of GameObjects with collision?
Hi,
I'm working on a 2d RPG with medieval combat mechanics. The weapons in my game have a hitbox trigger on the blade of them that attribute damage to any object or enemy with a health script. What I want to do is be able to attribute damage to however many enemies that come in contact with a blade at the same time. At the moment, it can only hit one enemy at a time. Can I populate an array with collided gameobjects then foreach attribute damage by accessing their components?
Thanks for any ideas!
Answer by maccabbe · Apr 16, 2015 at 10:01 PM
Yes, although you should probably use a hashset to store the things hit and immediately apply damage. Hashset is a structure that can hold a variable number of members (such as gameobjects) and is good for lookup and insertion.
The hashset would keep track of the objects hit. At the beginning of an attack empty the hashset. When a new collision is detected check the hashset for the object. If it is in the hashset then the other object has already been hit and the collision can be ignored. If it is not in the hashset then you should apply damage and add the object to the hashset.
https://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx
Thanks for the info, but this doesn't answer my question of how, specifically, to detect multiple colliders and apply damages. What to store them in is not of immediate concern.