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
0
Question by LoadStar · Jun 13, 2015 at 12:16 PM · physicstriggerexit

Is there a way to deny an exit (and therefore OnTriggerExit)

I would like to be able to tell when one object is trying to exit the collider of another and possibly block the motion that would cause the exit.

I have a system in which objects are sometimes able to be physically interacted with (stood on, bumped into, etc.) and sometimes not (when not, they can still be interacted with via other code, but not by physics), so the isTrigger property of some objects changes dynamically. My problem is actually that colliders sometimes do a isTrigger = false while a rigidbody is inside them which causes bugs. Generally, what causes other nearby objects to change state is entering/exiting a separate object.

So basically, entering object 1 causes objects 2 through N to change, like so...

 void OnTriggerEnter2D(Collider2D other)
 {
     foreach(GameObject o in specialInteractionItems)
         o.GetComponent<BoxCollider2D>().isTrigger = true;
 }
 
 void OnTriggerExit2D(Collider2D other)
 {
     foreach(GameObject o in specialInteractionItems)
         o.GetComponent<BoxCollider2D>().isTrigger = false;
 }

Some of these "special interaction items" are inside the collider of this, some of them are outside, and some of them overlap (part in, part out). If OnTriggerExit2D() is called while the player is currently overlapping (or even entirely within) a specialInteractionItem when that item gets isTrigger = true, that is the problem, as I then have my player (with its rigidbody) inside some other object which it normally cannot move into - the physics would normally have been disallowing this if not for the special interaction state.

It would be wonderful if Unity had some kind of bool OnAttemptedEnter() and bool OnAttemptedExit() which would disallow the translation if false is returned. If this exists, I want to disallow the above-mentioned exit, forcing the player to remain in a state where it is colliding with the other object. I have used other development environments which have this, and it is very useful. Does Unity have anything like this?

If Unity does not have this built in, I think I need to set up such a thing myself, but I would not know where to start doing this in Unity. Google was not much help, as it found plenty of results which had nothing to do with this.

Does anyone know how to do this, or does anyone have any recommendations or other ideas?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by zach-r-d · Jun 13, 2015 at 12:49 PM

Unfortunately, there are no such functions as you describe. Controlling entering an object is best done with collisions (achievable by toggling layer collisions on and off, disabling individual colliders, marking individual colliders as triggers, etc.), and controlling exiting an object is tricky.

To "deny exiting", there could be an EdgeCollider2D that starts disabled and is edited to have the same perimeter as the trigger; it could then be enabled when the player needs to be contained in the trigger.

Comment
Add comment · Show 1 · 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 LoadStar · Jun 26, 2015 at 12:18 PM 1
Share

Sorry I did not respond sooner.

Yes, what you describe could work. I started doing it in that direction according to your suggestion. Eventually I had an idea that works well for my case. Even though I deleted what I did with extra colliders as per your answer and made something else that I'm now continuing to use, I will still accept your answer, because it is also a correct answer. Thank you. I will provide another answer with what I am using now.

avatar image
0

Answer by LoadStar · Jun 26, 2015 at 12:43 PM

What I ended up doing was to keep track of the object's previous x/y every frame and move the object back if necessary.

I gave the mobile object which is to be disallowed from exiting a list of objects that it is currently overlapping. In its OnTriggerEnter/Stay/Exit I add/remove other. Let's call this list collisions.

The special object, the one which the mode ends when you exit it: let's call it Special.

In Special, I gave it a list of what the player will not normally be allowed to move through. This list needs to be populated with the objects that would normally block movement but which are currently not blocking movement due to the special mode. Let's call this list passThrough.

In Special's OnTriggerEnter I set player = other.gameObject; and a boolean exit = false; and for each object in passThrough I set it's isTrigger = true.

In Special's OnTriggerStay I have _x = other.gameObject.transform.position.x; _y = other.gameObject.transform.position.y;

In Special's OnTriggerExit I do exit = true and a foreach(GameObject o in collisions) and set a boolean block to true if I want to block the exit. Then, if(!block) I loop through passThrough again and put each object back to normal and set player = null.

In Special's FixedUpdate, I check if exit and player are set, and if both are then I just set player's RigidBody's location to the previous x,y that was updated in OnTriggerStay.

This does cause the game to constantly call Enter/Exit over and over again as it keeps the object in, but the player does not know that and it works well for my case.

Sorry for the sporadic code-bits instead of doing larger blocks of code. I have a bunch of related logic in mine which would have been extra baggage, so I didn't copy/paste. I might edit the answer to fix that if I have time after work; gotta run. Enjoy!

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

overlapsphere to destroy NPCs on exit 1 Answer

collider doesnt work the 2nd time 1 Answer

Pinwheel prevents player by moving 0 Answers

Collider/trigger collision causing physics glitch 0 Answers

How to make a trigger only affect the collided if its parents name is x 1 Answer


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