- Home /
Check that an object is within the bounds of a trigger, but not touching them?
I believe a similar question was asked here: http://answers.unity3d.com/questions/46199/determine-that-an-object-is-within-a-trigger.html
However, it didn't seem to get a solid answer, and it's over two years old. I would like to know if there is a way to determine that an object is inside of a trigger, but not actually touching the bounds of the trigger.
For example, I want to determine if my player is within a certain trigger, but the trigger is much larger than the player. Thus, OnTriggerEnter, OnTriggerExit, and OnTriggerStay only register that the player is "inside" if the player is touching the borders of the trigger.
Is there a way to do this without using multiple triggers?
The best that I can think is about using a Physics.SphereCast()
and making comparisons with hitInfo.
Could you elaborate on that, please? It's a function I've never used.
Also, in the Script Reference, the description for that function says that it can't be used with colliders that are configured as triggers.
The objects that come in contact to your trigger aren't triggers, right?
Let's make a example, if your player (with a collider) enters in the trigger with 2 in distance between the players and the center of the trigger, but with 1 in distance between both the player is completely inside of trigger. Just use a Physics.SphereCast()
with the center of the trigger with radius 1, if the player is in the hitInfo, so it's in inside in the trigger. You can also use the hitInfo to deter$$anonymous$$e the exact distance from the trigger center where the collision happens.
You may also check the bounds of the player collider (for the bounds attribute) to make sure that the object is inside of the sphere.
Answer by BSelvar · May 22, 2013 at 01:31 AM
I would use OnTriggerStay and a boolean variable that becomes true when the Player is within the object and then set that boolean to false with OnTriggerExit. Then I would apply effects to the Player in the update if that boolean is true. Something like this in C#:
private bool playerInBounds;
void OnTriggerStay(Collider Other){
if(other.gameObject.name == "Player"){
playerInBounds = true;
}
}
void OnTriggerExit(Collider Other){
if(other.gameObject.name == "Player"){
playerInBounds = false;
}
}
void Update(){
if(playerInBounds){
//effects to apply go here.
}
}
although, what do you mean exactly by "but not touching them?"
Answer by sparkzbarca · May 20, 2013 at 01:58 AM
oh this is very easy just do a distance check.
assuming you have for example (the easiest one) a sphere trigger.
If (vector3.distance(sphereTrigger.transform.position, player.transform.position) > sphere radius)
then they are inside the bounds of the sphere.
if you want to be more precise and catch them even when they are JUST BARELY inside it you'll need to take the players collider radius and subract it so you take the distance from the edge of the players collider and not the center.
Mark as answered please. :)
That definitely sounds like something that would work...for a trigger that is a sphere or a similarly open shape. But what of a trigger that has a very complex shape? That solution doesn't seem like it would work for what I have, since the central point of the trigger isn't even inside the bounds of the trigger itself:
http://i.imgur.com/AoFWoBa.png
I could set up multiple triggers inside of that one, all set to register that the player is inside that mesh, but I wanted to know if there was a simpler method that only uses that mesh. I'd like to leave this open a while longer to see if anyone can confirm that it's possible.
for that setup you have there you could simply
onTriggerEnter(collider object)
{
if(object.name == "player")
{
player.getcomponent.InBounds = true;
//alternatly store that someone is in the bounds in a variable
//in this script depending on which makes sense, for the player to
//know or the object
}
onTriggerExit
{
if(object.name == "player")
object.getcomponent.Inbounds == false)
}
they enter you store that as they are in bounds they exit you change the stored variable they are no longer in bounds
Answer by CubeFlix · Mar 18, 2018 at 07:02 PM
You can make invisible bounds, like with the Physics.OverlapSphere(), using it like this:
void Update(){
Collider[] thingsInBounds = Physics.OverlapSphere(transform.position, radiusOfBounds);
foreach(Collider thing in thingsInBounds){
if (thing.name == player.name) {
//Do results
}
}
Your answer
Follow this Question
Related Questions
Can I access a script OnTrigger WITHOUT using getcomponent? 1 Answer
How to use OnTriggerEnter with multiple triggered objects? 1 Answer
How to make multiple gameobjects instantiate from one collider? 3 Answers
issues with on trigger enter and exit 1 Answer
OntriggerEnter / Stay with the same Gameobject tags 0 Answers