• 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
Question by mitchmunro · Sep 22, 2021 at 03:15 AM · physicstriggertriggers

Problem: One Trigger activating a seperate Trigger it is not in contact with.

I have this problem that seems really counter-intuiative. There is a trigger on the bottom of a large crate (to test if it should crush an object it falls on), and another trigger on the bottom of the player model (to test if the player 'onGround').

 

When the player model enters the crate crush trigger, the player 'onGround' trigger is activated.

 

This is a big problem, because if the player gets hit by the crate crush trigger, the code checks if the player is 'onGround', and if so the player is crushed and gameover.

 

Here is a video of this problem in action: https://vimeo.com/611291709

 

Here is the trigger function on the player:

     //Check if player is on the ground
     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Ground") || other.CompareTag("BigObject"))
         {
             onGround = true;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Ground") || other.CompareTag("BigObject"))
         {
             onGround = false;
             canDoubleJump = true;
         }
     }

 

And here is the trigger function on the crate:

     private void OnTriggerStay(Collider other)
     {
         if (other.CompareTag("RegularObject"))
         {
             Destroy(other.gameObject);
         }
         if (other.CompareTag("Player"))
         {
             playerController = player.GetComponent<PlayerController>();
             if (playerController.onGround)
             {
                 Debug.Log("Player Crushed!");
                 GameManager.Instance.PlayerCrushed();
             }
         }
     }

   

Does the OnTrigger function get called on the player not only when their attached trigger is entered, but also when the player enters other objects triggers?

 

Is it better to use raycasts for some of these checks?

Comment

People who like this

0 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 Chimz · Sep 25, 2021 at 08:24 PM 1
Share

I think you might be having the same problem as this guy https://answers.unity.com/questions/1294855/is-there-a-way-to-make-ontriggerenter-work-only-fo.html

In that case, you need to separate your colliders with Empty Child objects

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Chimz · Sep 29, 2021 at 12:49 AM

Alright I think I got your answer now. Your problem is that even though you have codes for Colliders and Triggers (which work different), the fact that they're both Colliders at base and overlap can cause such bugs. To separate the two, it is best if you do this: You've put the Colliders on the Parent Object and the Triggers on child Objects. You've already done the first step. Now, on those child Objects you need to choose Specific Tags. Choosing which objects are Wakable with tags is not suggested. I suggest using Layers and setting them up in the Project Setting, because we need the Tags for other tasks. Now, you've set the layers and now it's time for the Specific Tags. For example, for the player Ground Trigger Object, just put "Ground Checker" and for the Box's crush trigger Object, put "Crush" Tag. Now, in your scripts, on the OnTriggerEnter's you need to check for the Tag of the Trigger that you've collided with. If it matches what you want, like having a Walkable Tag, then you can set your Grounded Boolean to True. If the tag is Crush, and the player is Grounded, then you Crush the player. Here's your Player code, edited to fit. But I have brought the "Crush Check" into the Player's Script for easier use: //Check if player is on the ground private void OnTriggerStay(Collider other) { if (other.CompareTag("Crush") && onGround) { Debug.Log("Player Crushed!"); GameManager.Instance.PlayerCrushed(); } }

     private void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("Ground") || other.CompareTag("RegularObject"))
         {
             onGround = false;
             canDoubleJump = true;
         }
     }
Comment
mitchmunro

People who like this

1 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 mitchmunro · Oct 12, 2021 at 05:37 AM 0
Share

This looks really great, thanks for all the effort you put into looking into this! I'll have a detailed look sometime, but it looks good!!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

OnTriggerEnter - race conditions? 0 Answers

Ignore collisions with self but still trigger with self 2 Answers

Is it possible to combine child trigger of a object to only trigger OnTriggerEnter once? 1 Answer

Sphere Collider set to Trigger acts as solid object 0 Answers

Triggers not being called 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