Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bunnynsnake · Jun 13, 2018 at 03:15 AM · triggerontriggerenterboxcolliderontriggerexitistrigger

Help with OnTriggerEnter issue

I want the player to walk through a wall then then not be able go though it again. So, i am using OnTriggerEnter on a box collider attached to the wall (gameobject) to have this work. everything is on the same layer, the player has a rigidbody, i am not sure why this is not working.

Here is the script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SetWallInactive : MonoBehaviour
 
 {
     [SerializeField] BoxCollider col_wall;
     void Awake()
     {
         col_wall.enabled = false;
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.tag == "Player")
         {
             col_wall.enabled = true;
         }
     }
 }

here is a video of what is happening....not sure why it isn't working. Problem video

any help is welcomed, this has been very difficult to try and figure out. Please let me know if you need to know anything else. @thetomfinch @Tobychappell @Feelnside

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 crawniik · Jun 13, 2018 at 03:25 AM 1
Share

I'm very much a newbie, but if the collider is set to inactive, can it read when something collides with it?

3 Replies

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

Answer by SlowCircuit · Jun 13, 2018 at 03:44 AM

  1. Does your player have a collider on them? If not there's nothing for the wall to collide with.

  2. I know for sure one reason why it's not working. In the other question/answer I said that you need the box collider to be ON. Take the part that disables it out of your code. isTrigger means the collider doesn't block the player, only triggers when they enter/stay/exit it. If the box collider is off, then it's not doing anything so it wouldn't ever trigger.

Comment
Add comment · Show 8 · 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 bunnynsnake · Jun 13, 2018 at 03:49 AM 0
Share

Yes the player has a ridgidbody and a mesh collider. i change the code to this

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SetWallInactive : $$anonymous$$onoBehaviour
 
 {
     [SerializeField] BoxCollider col_wall;
     void Awake()
     {
         col_wall.enabled = true;
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.tag == "Player")
         {
             col_wall.enabled = false;
         }
     }
     private void OnTriggerExit(Collider col)
     {
         if (col.tag == "Player")
         {
             col_wall.enabled = false;
         }
     }
 }


so, now it is active when the game is started, then the player goes through the collider and its set inactive and i am able to go though it, but it does not set it back active so its still the same issue, is it an issue with the OnTriggerExit?

avatar image bunnynsnake · Jun 13, 2018 at 01:24 PM 0
Share

ok, so I have tried this code,

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class SetWallInactive : $$anonymous$$onoBehaviour
  
  {
      [SerializeField] BoxCollider col_wall;
      void Awake()
      {
          col_wall.enabled = true;
          col_wall.isTrigger = true;
      }
  
      void OnTriggerEnter(Collider col)
      {
          if (col.tag == "Player")
          {
              col_wall.enabled = false;
          }
      }
      private void OnTriggerExit(Collider col)
      {
          if (col.tag == "Player")
          {
              col_wall.enabled = true;
              col_wall.isTrigger = false;
          }
      }
  }

It still is not having the desired effect, the wall box collider is active on start, then then the player walks through the box collider it becomes inactive and the player can go though it, but then it does not set the box collider active again. I tired to use Update() to check if the player has gone though the box collider and if so set it enabled but that did not work. any ideas ?

avatar image SlowCircuit bunnynsnake · Jun 13, 2018 at 06:20 PM 2
Share

Listen very, very closely cause you keep missing the most important part.

Remove all code from this script that uses .enabled. You want the collider to ALWAYS be enabled. Replace it with col_wall.isTrigger = false in OnTriggerExit(). You do not want any disabling/enabling code in OnTriggerEnter. $$anonymous$$ake sure that the collider in scene is enabled and isTrigger is true. You don't need to set anything in awake because it's already set in inspector.

Your code should look like this:

 using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class SetWallInactive : $$anonymous$$onoBehaviour
   
   {
       private void OnTriggerExit(Collider col)
       {
           if (col.tag == "Player")
           {
               col_wall.isTrigger = false;
           }
       }
   }
 
avatar image crawniik SlowCircuit · Jun 13, 2018 at 06:27 PM 0
Share

I meant to remove the awake method from the script I gave them... dangit

Show more comments
avatar image
1

Answer by crawniik · Jun 13, 2018 at 06:02 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SetWallInactive : MonoBehaviour
 
 {
     [SerializeField] BoxCollider col_wall;

     private void OnTriggerExit(Collider col)
     {
         if (col.tag == "Player")
         {
             col_wall.isTrigger = false;
         }
     }
 }

This should do what you want. It's working on my side.

Edit: Make sure the wall we're talking about has the Is Trigger option checked first though.

Comment
Add comment · Show 4 · 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 bunnynsnake · Jun 13, 2018 at 06:17 PM 0
Share

I will check it, thank you for working with me on this issue.

avatar image crawniik bunnynsnake · Jun 13, 2018 at 06:32 PM 1
Share

I honestly can't even take the credit. I only figured that out after reading @ZanyT 's answer.

avatar image crawniik bunnynsnake · Jun 13, 2018 at 06:34 PM 1
Share

If it still isn't working for you, make sure you put some kind of collider on your player. You can use mesh collider if it's not a basic shape like a sphere/capsule/cube etc.

avatar image bunnynsnake · Jun 14, 2018 at 01:59 AM 0
Share

Works perfect! thank you!

avatar image
0

Answer by ZanyT · Jun 13, 2018 at 03:34 AM

  1. The player needs to have a collider.

  2. In order for the wall to stop the player from walking through it, the collider needs to not be a trigger.

  3. You might run into issues with OnTriggerEnter blocking the player immediately: if so, change it to OnTriggerExit.

My suggestion would be to start with the player having a non-trigger collider, and having the wall with an is-trigger collider. You need both colliders enabled. Then, in OnTiggerExit, set the walls collider isTrigger to false in the code, and the two non-trigger colliders should prevent the player from going back through the wall.

With the way you have it right now, with a disabled collider, it's as if that component isn't on the object at all, so it has no way to detect collision as it is.

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 bunnynsnake · Jun 13, 2018 at 03:40 AM 0
Share

the player does not have a is trigger on it, only the wall does. and how would i set the walls collider isTrigger to false in the code? also if i have the collider active the player would not be able to walk through the wall, so would i have to set the wall collider inactive when the player enters the collider, and then set it active when they exit?

avatar image ZanyT bunnynsnake · Jun 16, 2018 at 02:06 AM 0
Share

A collider that is set to "Is Trigger" does not actually collide with anything. It just detects things that go through it. In the code, let's say that your collider is called "col" you would say: col.isTrigger = true; Essentially ins$$anonymous$$d of enabled and disabled the collider, you need to change the isTrigger between true and false. And both the player and wall need a collider.

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

106 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 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 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 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 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

Box collider with strange behaviour 1 Answer

issues with on trigger enter and exit 1 Answer

Compound Triggers with no intermediary exit calls 1 Answer

Trigger on MeshCollider Only Seems To Work at Edges of Mesh 1 Answer

OnTriggerEnter/Exit called unexpectedly 2 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