How do I find out if the entirety of a collider (all of it’s points) are inside a trigger? This so that that the code inside the trigger will only fire once the collider is completely inside.
For discussion: “Trigger” = the larger “range” collider and “Collider” = the object you are trying to detect.
First, I should point out that if the trigger is a sphere, making it smaller should work pretty much the same way, though I guess if the colliders you are trying to detect are different sizes, and that is important, then you need the solution you are asking about.
One workaround might be to use 4 really small colliders placed around the object and don’t fire unless all 4 enter. Workarounds really depend on exactly what you are trying to do in your game. I know this doesn’t answer your question exactly, but it doesn’t hurt to think outside the box a little if you don’t really need the perfect solution.
Back to the original question… What about the other way around? If a collider passes through ‘the wall’ of another collider, does it trigger the exit event (it has been a year since I touched it but I think it might)? If it does, you could compare the two: if the collider triggers on exit, but it is still inside the trigger, then that would do it… does that make sense?
If you want to verify one side of the trigger, you can use two triggers - pre and main. When the object leaves the pre trigger AND is inside the main trigger you can fire your action.
Create a thin pre trigger (about 1 to 2 units thick) and place it right before the main trigger, then attach this script, called PreTrigger.js:
var exitCol: Collider;
function OnTriggerExit(col:Collider){
exitCol = col;
}
And attach the following script to the main trigger:
var preTrigger1: GameObject; // drag the pre-trigger here
function OnTriggerStay(col:Collider){
var preScript = preTrigger1.GetComponent(PreTrigger); // get the pre-trigger script
if (col == preScript.exitCol){ // this object left pre trigger?
preScript.exitCol = null; // avoid repetition
// the object is completelly inside; do whatever you want here
}
}
You may also have other pre-triggers for other sides - just create additional variables preTrigger2, preTrigger3 etc. and repeat the code for each one in the OnTriggerStay event.
a more complete (and more complex) solution would be to check if your collider is on the trigger (OnTriggerStay) overlap and compare the bounds’s position with the trigger position.
This requires much more tweaking, so if you don’t need that much precision, Aldo’s multiple trigger can do the trick, and since you can use TriggerExit and TriggerEnter, it is less costly.