Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Eidern · Apr 05 at 05:02 PM · collider

How to prioritize colliders?

Hi, I've got a Object with a rigid body (so that does manage the collision triggers), Sometimes it will collide with 3 different GameObjects at the same time . How can I tell it to forget about the other colliders if one of the 3 got a priority on others?

I'm working with the tags so far, but it seems that the collisions does not care about the order in the code:

 private void OnTriggerEnter(Collider other)
     {
         if (other.transform.parent.tag == "Obstacle")
         {
             //PRIORITY CODE is not executed in first
 
         }
      
          else   if (other.transform.parent.tag == "Enemy")
             {
          //SECONDARY CODE, but executed first :/
 }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MKFusion · Apr 05 at 07:55 PM

Hi I know it's not a perfect solution, but I had a similar issue few days ago.

One way would be to set a private flag variable, "hasColided" for example. Then on Trigger you just check at the beginning if the other collided object has that flag set to true.

Comment
Add comment · Show 3 · 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 Eidern · Apr 06 at 01:31 PM 0
Share

HI, thanks for you answer I don't know if I understand well your private flag variable concept.. Are you talking about some private var inside the class? For instance in my project I ve already got this, the object that have got the rigidbody, collides at first with 3 GameObject tagged "enemy", in order to only collect with the first enemy, I've got a bool "hasBeenTriggered" false at start and in the code :

  private void OnTriggerEnter(Collider other)
     {
         if (hasBeenTriggered)
             return;
 
 if (other.transform.parent.tag == "Obstacle")
         {
             hasBeenTriggered = true;
          Debug.log("Obstacle Trigger");
         }
      
            else  if (other.transform.parent.tag == "Enemy")
             {
             hasBeenTriggered = true;
 Debug.log("Enemy Trigger");
         }

But in the above example, even if 3 Enemy and One obstacle are at the exact same place, it will only Debug "Enemy Trigger" (once as expected); But I want to prioritize the Obstacle first.. It seems that the enemy tag is triggered first prior to the Obstacle tag.

avatar image MKFusion Eidern · Apr 10 at 10:23 PM 0
Share

But "Obstacle Trigger" is called as well eventually ?

avatar image Eidern MKFusion · Apr 11 at 05:37 AM 0
Share

yes, if there's no enemy that trigger the same collider at the same time. Imagine a 2d plane, where enemies run over it. your mouse cursor is an aim, and when you click it it instantiate a bullet object (the one with the Rigidbody with this code on it) for 0.1sec, if it makes a collision, the bullet object is destroyed. if this bullet intersects with 4 enemies (4 at the same place they overlap), it will collide with one enemy (thanks to the hasbeenTriggered). If the bullet touch an obstacle (which enemies are supposed to take cover behind) it collides with the obstacle as expected.

But when an enemy and a an obstacle are at the same spot, where my code logic would tell the bullet to collide with the obstacle, in this case it collides with the enemy. Somehow the triggers on the enemies are called first.

I've made a workaround for this actually, with the enemies disabling their colliders when they enter an obstacle collider, but this involves an unnecessary collision check.

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

178 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

Related Questions

Internal collisions 1 Answer

void' is not an iterator interface type with collider "ERROR" 1 Answer

Objects not jumpable on 2 Answers

Using raycast and collider to increase int 1 Answer

OnCollisionEnter not triggering when two rigidbody collide via Instantiate 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