Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 stfarless · Jan 06, 2017 at 08:44 AM · collidersontriggerenterbasics

Is there a way to make OnTriggerEnter work only for the colliders marked as triggers?

I've looked around, but I haven't quite found my question. I'm making a very simple game and one of the aspects of the game is pushing and pulling a box. I got that part figured out, but currently if the player stands on the box, they can pull it around. I tried fixing this by making the trigger close to the ground, but, I guess, on trigger enter works for colliders that aren't marked as triggers as well.

I feel like I'm missing something obvious. I could, probably, just put this behavior in a child object and put the trigger there. But I want to know if there's a smarter solution.

Relevant Code:

 void OnTriggerEnter2D(Collider2D other){
     if (other.tag == "Player") {
         near = true;
         //Debug.Log ("Near is:" + near);
     }
 }
 void OnTriggerExit2D(Collider2D other){
     if (other.tag == "Player") {
         near = false;
         //Debug.Log ("Near is:" + near);
     }
 }

Screenshot of Colliders: alt text

theproblemathand.png (27.2 kB)
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 stfarless · Jan 06, 2017 at 08:13 PM 0
Share

For people wondering, I want to mention that OnTriggerEnter calls for any trigger, whether or not it belongs to the object, so my player's trigger was calling the function. I don't know the fix for this (perhaps simply a very small trigger on either side of the box and putting the functionality on those). I don't need a trigger on my player for this game, so I simply removed it.

2 Replies

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

Answer by tanoshimi · Jan 06, 2017 at 08:52 AM

OnTriggerEnter does only fire when a collider enters another marked as a trigger. It looks from your screenshot that you have both a trigger and a non-trigger collider on the same object. This makes a compound collider - effevely merging the bounds of the colliders into one.

To keep them separate, add the collider components to separate child objects and/or place them on separate collision layers.

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 stfarless · Jan 06, 2017 at 07:34 PM 0
Share

$$anonymous$$y dude, my pal, my friendo. This is exactly what I was looking for. I figured that makin one a child object was the solution. Thanks for the info on compound colliders. Hadn't heard of that, but it explains so much!

avatar image
3

Answer by AurimasBlazulionis · Jan 06, 2017 at 08:55 AM

If you need to make it work only with triggers, then just check if the other collider is a trigger using other.isTrigger. Problem solved. For more advanced control you will need to look into collision matrix.

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 ManuGamesDev · Feb 20, 2019 at 04:23 PM 0
Share

thanks that helped me!

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

6 People are following this question.

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

Related Questions

OnTriggerEnter question 1 Answer

OnTriggerEnter is triggering from colliders on child objects 0 Answers

Can't call GetKey inside OnTriggerEnter? 2 Answers

How Does OnTriggerEnter() Work? 0 Answers

OnTriggerEnter/Exit not working 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