- Home /
OnTriggerStay with multiple collision parameters
I have a GameObject that needs to detect collisions with 2 other GameObjects simultaneously.
I need to be able to store the 2 GameObjects within 2 variables, which will be used for referencing later on.
The following:
void OnTriggerStay (Collider object1, Collider object2) {
}
does not work, since the OnTriggerStay function only takes 1 or 0 parameters.
To also clarify, I need to use OnTriggerStay because I need to consistently check for the collisions.
Any solutions?
Answer by jdean300 · Jul 19, 2016 at 09:18 AM
Keep a list of the objects that have collided with your main object. Check that list every FixedUpdate for the items you want to detect simultaneously, then clear the list:
private HashSet<GameObject> m_CollisionsThisStep = new HashSet<GameObject>();
private void FixedUpdate(){
bool hasObjectOne = false;
bool hasObjectTwo = false;
foreach(GameObject go in m_CollisionsThisStep){
//Set the bools above as you loop through and check the traits of the game objects
}
if (hasObjectOne && hasObjectTwo){
//Execute your logic for simultaneous collision here
}
m_CollisionsThisStep.Clear();
}
private void OnTriggerStay(Collider c){
m_CollisionsThisStep.Add(c.gameObject);
}
HashSet is not a known keyword for me.. is there any imported attributes or derivatives that I have to add to my monobehaviour?
It's in the Systems.Collections.Generic namespace. So you need to add:
using Systems.Collections.Generic;
https://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx