Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Kage Ryu · May 19, 2013 at 03:33 AM · ontriggerenterontriggerexitontriggerstayinsidezone

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FL · May 19, 2013 at 05:01 PM 0
Share

The best that I can think is about using a Physics.SphereCast() and making comparisons with hitInfo.

avatar image Kage Ryu · May 20, 2013 at 01:43 AM 0
Share

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.

avatar image FL · May 22, 2013 at 12:29 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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?"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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. :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kage Ryu · May 20, 2013 at 02:59 AM 0
Share

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.

avatar image sparkzbarca · May 21, 2013 at 09:57 PM 0
Share

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

avatar image
0

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
               }
        }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges