Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by ultramanjones · Nov 27, 2014 at 11:37 PM · collisiondetection

onTriggerEnter2D - How to know BOTH colliders involved?

Hi,

I have just started using Unity 2D. What I am attempting to do is add a CircleCollider2D component to my player but with a large radius and use it as "aggro" detection. In other words when the player's CircleCollider2D collides with an enemy's BoxCollider2D it triggers its EnemyAIScript making the enemy begin chasing and shooting at the player.

Unfortunately, the only parameter that onTriggerEnter2D gives me is the OTHER collider.
Since the player also has a BoxCollider2D around his body and I have no way of determining which collider did the colliding, I suddenly have a dead player (since colliding with anything that "isTrigger", such as enemies, triggers death.)

Is there any way to find out the identity or type of the local collider? Obviously, I could do proximity checking lots of ways, but it seems a shame if I can't do it this way because it would be so easy and (I think) efficient. Any help or insight is appreciated. Thanks!

Comment
Add comment · Show 4
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 ultramanjones · May 28, 2014 at 09:07 PM 0
Share

I know that the parameter is a reference to the other object. $$anonymous$$y question is HOW can I get the identity of the local collider that is involved in the collision, because it is not given in onTriggerEnter2D. Any method would be welcome.

avatar image fafase · May 29, 2014 at 07:10 AM 0
Share

Your writing is in one block which makes hard to read.

As for your answer, remove the circle collider and use a distance detection ins$$anonymous$$d. The efficiency won't get hurt as using Vector3.Distance is pretty fast and efficient.

What you are after is just micro-optimization which is not required at the beginning of a game, particularly when it does not change much or anything.

avatar image nnurgle · Nov 27, 2014 at 11:37 PM 0
Share

I did some testing and here is my "new to unity" attempt at answering this...

1) Create a child object of your character

2) Add the trigger collider to that object, not your character

3) Add the script to handle the OnTriggerEnter2D to that object, not your character Since there is only one collision object, you know that it is your trigger

4) Add a public variable in your script that represents your character object in the inspector, drag your character node into the public variable location for your child node's script component if you need access to your player. (to set any variables etc...)

That should do it.

I did a quick test with my project and this seems to work for me.

avatar image meat5000 ♦ · Nov 27, 2014 at 11:38 PM 0
Share

I did a little cleanup here. Let's just say on track, eh? :)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Kiwasi · Nov 27, 2014 at 11:42 PM

Easiest way to solve is put a child GameOObject with the appropriate separated colliders. Then put the OnCollisionXXX on a script on the child GameObject. Then you can simply use the name of the GameObject to figure out which one was activated.

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 dynamicvoltagegames · Oct 04, 2019 at 09:45 PM 0
Share

that will only show one collider. He wants both. But regardless, what happens is that the child is going to combine with the parent to create one single collider, so it cannot be separated.

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

24 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

Related Questions

Testing for collisions prior to placing an object 0 Answers

Collision between capsule collider 0 Answers

Collision detection of fast rigidbodies 0 Answers

clarification on collision contact points 1 Answer

Imported Blender model causes strange collisions 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