- Home /
Distinguish between multiple box colliders?
I have a child object called LedgeDetection with 3 Box colliders. The plan is if the bottom 2 are touching a wall bot the top is not, it's a ledge that can be grabbed and climbed onto.
The problem is when touching a wall, all 3 fire OnTriggerStay at once and it's impossible to tell them apart. I can't think of a good method of doing this. Should I check their local coordinates to distinguish them? Put them all on separate child objects with separate scripts containing OnTriggerStay?
Or am I just doing this completely wrong and there's an easier/better solution?
Thanks!
Answer by Denvery · Jun 28, 2014 at 08:28 PM
Hello, Juice-Tin! As for me there is 3 ways to distinguish the colliders.
Yes, you can put them on separate child game objects and create new scripts with
OnTriggerStay
function. It will be useful especially if you are planning to use their separately in future and to implement the other functions (`OnStart, OnTriggerEnter` etc.)May be more elegant and shorter way. Create 3 public fields in your current script (where you implement OnTriggerStay now):
public BoxCollider collider1;
public BoxCollider collider2;
public BoxCollider collider3;
These girls will appear in the inspector. Then, assign your attached colliders for these fields. So you can distinguish the colliders in OnTriggerStay in the manner:
private OnTriggerStay (Collider other)
{
if (other == collider1) ....
}
And please do not check local coordinates to distinguish because it is straight way to non-modifiable code:-)
Hope this answer will help you a bit:-)
Hah, public variables. How did I not think of that! Thanks, I feel silly now lol.
Sorry that I described so detailed, it was enough to write one word "public var" ;-)
$$anonymous$$aybe it was to create an array of BoxColliders and use GetComponents<BoxCollider>();
to initialize the array?
i have the same issue when i use- if (Collision==BoxCollider1), it doesn't work. i'm guessing it's because collision is referring to the other game object, therefore i cannot distingusih between BoxCollider1, 2 or 3. Please help
my sprite has two colliders one for the enemy to know when its in range and the other for the player to take damage and pickup items. i created an empty child gameobject and put in the range test collider and tagged it as range but still Ontriggerenter2D still activates for the child object. i need a solution
Hi @Denvery ! Can you help me to solve this similar problem? https://stackoverflow.com/q/50564752/6439069
Did you mark the collider to be a trigger one in the Editor? If yes, and the script is enabled (this.enabled == true) so $$anonymous$$onoBehaviour calls fire, add some debug code lines for other.name, other.gameObject, etc to see if the event is called at all and if it was, what Collider detected the collision. BTW your "if (other == cCollider) {" will never be true if cCollider is not a trigger one, and that's what I assime from your description. As OnCollisionEnter != OnTriggerEnter.
Answer by Lylek · Jun 28, 2014 at 09:17 PM
Have you tried raycasting, instead? You can have 2 child objects as transform vars to reference. Then raycast forward from their positions. If the bottom one hits something, but the top one doesn't, then climb.
var object1 : Transform;
var object2 : Transform;
var hit1 : RaycastHit;
var hit2 : RaycastHit;
function FixedUpdate() {
//Projects a raycast from object1 forward, 3
if(Physics.Raycast(object1.transform.position, transform.forward, hit1, 3)) {
//If first raycast hits, raycast 2nd
if(Physics.Raycast(object2.transform.position, transform.forward, hit2, 3)) {
//if 2nd hits, you're at a wall
Debug.Log("Wall");
}
else {
//if 2nd does not hit, climb
Debug.Log("Climb");
}
}
}
Yeah, I'm thinking I might have to use Raycasting anyway.
With Raycasting his lefT+right side I'll be able to find the angle of the object, so I can make him face it perfectly. Colliders will tell me an object is there, but not the angle it's at.
Thanks!