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
1
Question by mustafa · Jul 01, 2015 at 09:20 AM · collisioncollidercolliderscollision detectioncollisions

Why there is no Collider.IsTouching(...) ?

In the unity 2D api there is a **Collider2D.IsTouching** and **Collider2D.IsTouchingLayers** methods, but there no such method for the 3D Collider, Why?

This really useful for checking if player is grounded with one line of code:

 bool grounded = collider2d.IsTouchingLayers(groundLayer);

You can do it with OnCollitionEnter and Exit and Stay but its a mess and you need to have code on the object its self.

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

2 Replies

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

Answer by Baste · Jul 01, 2015 at 11:21 AM

Unity uses third party packages for both 2D and 3D physics. For 2D, the underlying physics is driven by Box2D, while for 3D, NVidia's PhysX is used.

So the things you have access to in your C# code is essentially wrappers for calls to those two libraries, or whatever Unity has bolted on top of those. I'm assuming that Box2D has an IsTouching call, while PhysX doesn't.

Now, I imagine that collider.IsTouching doesn't exist because it would be somewhat expensive. Either you'd have to have a list of all the things everything touches (which would cost TONS of memory, and make the library useless), or you'd have to scan the object for overlaps, which would probably be very slow.

Essentially, if the method existed, the docs would probably state "you should not use this in Update, because it's super-slow". OnCollisionEnter/Exit does not have any of those drawbacks, so.

2D checks for overlaps are probably a lot faster (I'd imagine by an order of magnitude), so it's viable to have an IsTouching method.

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 AlwaysSunny · Jul 01, 2015 at 01:52 PM 0
Share

Good Answer, @Baste.

@ OP: I've found that in practice, it's pretty easy to overcome this limitation on a case by case basis (and only use said workarounds when necessary). Occasionally your alternatives will feel somewhat convoluted, but by and large, your problem solving style will adapt to Unity's quirks.

avatar image
1

Answer by Phildo · Nov 05, 2019 at 04:35 PM

I know this is old, but re: "you'd have to have a list of all the things everything touches (which would cost TONS of memory ...)"- I think it MUST already do that? Otherwise, how does it possibly know to call onTriggerEnter and onTriggerExit? (Both of those mark a transition of states, meaning you have to know the previous AND the current state to detect it- ie, a "list of all the things everything (currently) touches").

I don't actually know the underlying code, so maybe there's some clever trick that gets around this? But this constraint (requiring current state) feels pretty airtight...

Anyways- the point of the question was "WHY is there no colliderIsTouching".

One answer: bc the underlying physics implementations don't have such an API. (Which just kinda launders the question? Like, why doesn't the underlying physics implementation offer it? Why hasn't unity built one on top of the underlying physics implementation? Why not, at the very least, just expose that list which it surely must be storing somewhere [I'm like, 95% confident hah]?)

Another answer: bc it would be slow. But that's pretty... relative? Like you don't want a physics world built on one-off collision detection. But often, there's a conditional tree behind needing to detect 3d collision for one frame on one pair of objects, and managing some physics heirarchy for that is slow, error prone, and unnecessarily complex!

tl;dr: the current answer for "why" might be correct, but it's not exactly satisfactory. If there exists a reason that takes into account these listed concerns, I'd love to hear it!

Comment
Add comment · 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

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

Strange issue with collider 1 Answer

How to reliably break contact/collision? 0 Answers

Detect if a non-trigger collider is inside another collider 1 Answer

How to detect collision between 2 objects while checking are they the ones that need to collide? 1 Answer

How to get OnTriggerEnter effect without using isTriggered on Collider? 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