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?
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:
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");
}
}
}