Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by shermenz · Sep 06, 2016 at 01:41 PM · ontriggerenterfirst-person-controllerontriggerexit

Character controller trigger enter and exit loop

Hey everyone. I'm a beginner, I'm trying to figure some first person controller stuff.

There's a cube acting as a wall. What I'm trying to do is get the character to move through the wall, the wall's layer change to "PassThrough", allowing the character to pass through it, and the wall color to change, and when the player exits the wall, those same parameters to go back to their default state.

I had no problem achieving both goals by using OnTriggerEnter and OnTriggerExit.

But it seems these 2 events are getting called again and again while the player is inside the wall, meaning the wall will rapidly change from (let's say) green to white.

What am I missing? Thanks.

Comment
Add comment · Show 1
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 ScaniX · Sep 06, 2016 at 01:57 PM 1
Share

If the wall collider is a trigger (as it seems to be as you are using the OnTrigger() methods), isn't the player able to pass through it anyway?

Also I guess those methods are called repeatedly, because OnTriggerExit() is called by the physics after disabling the collision (by switching the layer) which will change the layer again which will cause OnTriggerEnter() to be called which will disable... ;)

Haven't tested it, But if I remember correctly, the Exit() methods are called in case of disabling the collider.

1 Reply

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

Answer by iwaldrop · Sep 07, 2016 at 04:58 AM

You'll likely want a trigger collider attached to the player which will trigger monobehaviours on other game objects. To keep things simple, just create a child game object of you player and attach a sphere collider set as a trigger. Create and attach a script to this game object which will does the following:

(psudocode that probably won't work as is)

 void OnTriggerEnter(Collider other) {
    TryTrigger(other.gameObject, enter: true, sender: this) // or sender as player?
 }
 
 void OnTriggerExit(Collider other) {
    TryTrigger(other.gameObject, enter: false, sender: this) // or sender as player?
 }
 
 void TryTrigger(GameObject otherGameObject, bool enter, Object sender) {
    var triggerable = otherGameObject.GetComponent<Triggerable>()
    if (triggerable != null) {
       triggerable.Trigger(enter, sender)
    }
 }

Then create an abstract Triggerable class which inherits from MonoBehaviour which defines the contract which all triggerables will implement. In this simple example, you'll need two classes:

 public void abstract class Triggerable: MonoBehaviour {
     public void Trigger(Bool enter, Object sender);
 }

 public class PassThroughWall: Triggerable {
    public void Trigger(Bool enter, Object sender) {
       // do the things that you're currently doing in your existing script
    }
 }


Now just attach the PassThroughWall script to your wall section which you want to pass though and try to walk though it. I haven't tested this code so you'll likely have to tweak it slightly, but the idea is a solid, simple, way to approach the problem.

You can now make all sorts of things triggerable by the player approaching. I'd recommend enhancing this technique and put the trigger and triggerable game objects on special layers which will reduce the overhead which would be caused by the collider calling get component on every game object that it collides with.

Good luck!

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 shermenz · Sep 07, 2016 at 09:20 AM 0
Share

Thank you for the well written and thorough reply, it works flawlessly!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Text disappear on OnTriggerExit() 1 Answer

How do I destroy an instantiated game object when player leaves trigger area? 1 Answer

How can I make a collider that gives me stat up to stop giving me it? 0 Answers

math.Lerp not correct? 1 Answer

Im trying to turn off a sound when i leave a trigger collider 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