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 /
avatar image
1
Question by gaetanspada · Dec 13, 2019 at 02:33 PM · triggerboxcollider

is it possible to not get triggered by your own BoxCollider ?

I have a problem, i have 2 car (the red one is my player) they both have a boxCollider attache to their gameObject and i want to stop the car who enter the boxCollider of the other one (because the red is faster than the grey). Here is an exemple schema:

alt text

I want my red car to stop when it enters the Collider box of the grey car but not my grey car to stop. I would also like the grey car to stop when it enters the Collider box of my red car ( player).

The problem is when the red car enters the boxcollider of the grey car both stops because the boxCollider also triggers the grey car and not only the red one. I would therefore like the trigger of the boxCollider not to trigger the GameObject with which it is associated. Is that possible? Or is there an oder solution

Here is the code of the red car(player):

     void OnTriggerExit(Collider other)
     {
         if (other.tag == "TrafficCar")
         {
             targetSpeed = 14f;
         }
 
     }
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "TrafficCar")
         {
             targetSpeed = 0f;
         }
 
     }

the grey car have the same code, only the tag change. Any help would be aprecieted !

car3.jpg (51.6 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GanemVisk · Dec 13, 2019 at 02:41 PM

Have a reference to your own collider and check if it's it.

 public Collider ownCollider;
 
 private void OnTriggerEnter(Collider other)
      {
          if (other.tag == "TrafficCar" && other != ownCollider)
          {
              targetSpeed = 0f;
          }
  
      }

Edit: or if the collider is in the same game object as the script you can check that (which was the question): other.gameObject != gameObject

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 gaetanspada · Dec 13, 2019 at 03:17 PM 0
Share

it could have been a good idea but unfortunately it doesn't work...

avatar image GanemVisk gaetanspada · Dec 13, 2019 at 03:24 PM 0
Share

I tested a simplified version and it worked. Did you put the Collider in the ownCollider box in the editor?

avatar image gaetanspada GanemVisk · Dec 13, 2019 at 04:23 PM 0
Share

Ofc, but I have an old version of unity (5.6.2) due to old asset and I can't upgrade. But I will dig a bit more on this one.

avatar image Larry-Dietz · Dec 13, 2019 at 04:30 PM 0
Share

The problem with this solution, is it assumes the car may be colliding with itself, and this will solve that problem, if that is the case, however, I don't think this is the problem that is going on. When an object moves into a trigger collider, BOTH objects will get the OnTriggerEnter, so the grey car will see the red car colliding, and the red car will see the grey car colliding. The problem to solve here is how to deter$$anonymous$$e which car needs to slow down, when both cars are receiving the trigger.

avatar image
0

Answer by Larry-Dietz · Dec 13, 2019 at 03:47 PM

Easiest way I can think of to handle this, without a ton of velocity checks, etc... would be to modify your OnTriggerEnter function to see if the colliding object is in front of or behind your transform position, and only run your slow down code is the colliding object is in front of your position.

This way, using your image as an example, Grey car detects trigger, sees collision is occurring behind it, discards collision(does nothing) Red car detects trigger, sees collision is occurring in front of it, slows down.

Hope this helps, -Larry

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 gaetanspada · Dec 13, 2019 at 04:29 PM 0
Share

Great idea but i am a newbi in unity... Do you have and example code of this case ? This could be very helpful !

avatar image Larry-Dietz gaetanspada · Dec 13, 2019 at 04:38 PM 0
Share

Is this an endless runner type game? Are the cars always moving in the direction of your image?

If so, assu$$anonymous$$g this is a 2D game, like image, then you can just compare x positions. Something like this...

 private void OnTriggerEnter(Collider other)
 {
          if (other.transform.position.x < transform.position.x)
          {
              targetSpeed = 0f;
          }
  
 }
avatar image gaetanspada Larry-Dietz · Dec 13, 2019 at 04:44 PM 0
Share

Tank you ! No, it's a 3d simulation and the grey car is the traffic (~1000 other cars like this in the simulator )

Show more comments
avatar image
0

Answer by Philosophbot · Dec 13, 2019 at 03:27 PM

In the if statement you can check to see if the velocity of the car is greater than the one that you are colliding with. private void OnTriggerEnter(Collider other){ if (other.tag == "TrafficCar" && this.getComponent().Velocity > other.gameobject.getComponent().Velocity){ targetSpeed = 0f; } }

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 Philosophbot · Dec 13, 2019 at 03:30 PM 0
Share

or if you aren't applying forces you can just check to see if the targetSpeed value is greater than the other cars targetSpeed value.

avatar image gaetanspada Philosophbot · Dec 13, 2019 at 04:27 PM 0
Share

Great idea thank you, I will try that !

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

143 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

Related Questions

Check if the center of an object is inside a trigger Collider? 1 Answer

How can I make a trigger detect Terrain? 0 Answers

Instantiating and deleting objects with triggers problem 0 Answers

How to check if a BoxCollider is hit by a trigger? 1 Answer

Box collider with strange behaviour 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