How can I collect collider2Ds(Trigger) the player is in into an array
I'm now trying to figure out how I can make a script that collects any game objects with triggers, with the tag ground that the player is currently in. So if the player is in one ground trigger it is collected and stored in the array and if the player is not in the trigger the trigger is removed from the array. Ultimately a variable will be collected from the objects in the array which will then be sorted in highest first. Can anyone come up with a script that can do this?
Answer by BradyIrv · Jun 11, 2019 at 04:00 PM
public class GroundBlock : MonoBehaviour {
public int height;
}
Private List<GroundBlock> grounds = new List<GroundBlock>();
OnTriggerEnter(Collider other) {
if(other.tag == "ground") {
groundComponent = other.GetComponent<GroundBlock>();
if(groundComponent != null && !grounds.Contains(groundComponent)) {
grounds.Add(groundComponent);
grounds = grounds.OrderBy(g => g.height).ToList();
// grounds = grounds.OrderByDescending(g => g.height).ToList();
}
}
}
OnTriggerExit(Collider other) {
if(other.tag == "ground") {
groundComponent = other.GetComponent<GroundBlock>();
if(groundComponent != null && grounds.Contains(groundComponent )) {
grounds.Remove(groundComponent);
}
}
}
Ultimately the main question is sorted, the other bit is a sort of something like this:
848943 To this:
988443 This doesn't really need to be answered as a actually decent sorting algorithm shouldn't be too hard to find.
Never$$anonymous$$d turns out I am having some troubles.
Each Ground collider Has an attached script which assigns the ground collider's height variable. which is an Int. Its that Int that should be sorted and then returned after the ground colliders are collected.
Sorry to bother you again.