- Home /
How to force an object to stay inside a trigger collider?.
Title says all, I need to be able to force a game-object to stay within the boundaries of a set of colliders, it must be able to exit one trigger collider and enter another one if they are overlapping, but not to ever be outside the boundaries of a trigger.
How should I go about this?, an if statement checking if it's still inside the a trigger, and then pushing it back if it isn't, would that work?, it needs to be subtle enough that it won't jitter when it tries to escape the boundaries, and simply act as if there were a wall there.
One thing to note, triggers only fire when you touch them, and stop touching them. They do not tell you when part of you leaves them. They make good "intruder detectors" but bad prison alarms.
You can shrink the trigger to compensate, but you often can't make a "check when part of you leaves this area" using triggers, esp, for oddly shapes.
Answer by ricardo_arango · Jul 10, 2014 at 09:32 PM
You could surround the object with non-trigger Colliders when it enters the trigger object. If you have an overlapping trigger then you would not create a boundary collider for that section where the triggers intersect.
Another approach would be to have code that checks for when the object tries to exit the Trigger volunme (OnTriggerExit) and then force the object's position back to where it was in the previous FixedUpdate, back inside the trigger. In this case to allow the objects to pass to the next trigger you would have a check in the OnTriggerExit to see if the object has entered the other trigger (which should happen as they overlap, right?).
I would recommend the first method as it's simpler.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153744im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to change material color to a specific object group's childs..? 1 Answer
Fading groups of objects to black 1 Answer
How to know if a collider has a rigid body inside it or not? 1 Answer
How to use Network.Instantiate Groups 1 Answer
Tween animations when adding / removing from layout groups 0 Answers