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 dan5071 · Nov 09, 2018 at 11:54 PM · collisionperformancecollision detection

Collision Performance Question

I can't seem to find many details about how collision detection in Unity works under the hood in the official documentation or from Google, so I figured I'd ask here. Do methods like OnCollisionEnter() behave more like the Update() function where the engine checks for a collision after so many frames, or does it behave in a more "event-based" way so calculations are only performed after two colliders overlap?


The point behind this question is I'm designing a game with a player that collects a variety of different pickups by colliding with them, so I'm faced with the following two options:


  1. Have all collision detection handled by the player since nothing else interacts with these pickups.

  2. Handle collision detection in the individual pickup scripts to determine what happens when the player comes in contact with them.


The first option obviously is much better performance-wise if OnCollisionEnter() behaves similarly to Update() since it eliminates multiple objects all trying to check the same thing every couple of frames. But the second option has the benefit of creating a more modular approach where there is a clearer separation of the actions being performed.

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
1
Best Answer

Answer by JVene · Nov 10, 2018 at 01:46 AM

The physics engine used in Unity is PhysX. The exact version depends on what version of Unity you use, but probably 3.2, or at least 3.2 documentation is largely applicable. Be aware, however, that while you can gain a great deal of detailed information by searching for PhysX information, the functions Unity exposes are named by Unity, and so you won't find exact matches for function names, nor is the entire physics engine exposed by Unity (though most of it is).


That said, collision detection is quite a science. PhysX uses a lot of optimization techniques. The cycle of the physics engine is associated with the Fixedupdate function, not the update function, and happens at a fixed time interval you can configure. Since you don't give volumes, I would offer only that if a single object is being tested for collisions against about 50 objectives, performance will be quite good, and the engine is designed to handle such volumes.


That said, if you're up to it, making your own detection system might be faster, but not necessarily by much. This is, in part, because what you write will run in C#, while PhysX is written in C++. Your primary advantage is that you can greatly simplify the detection, as in not creating a full fledged collision system, but merely a search method which determines if your player is in contact with a target. You would do well to mimic the broadphase of the physics engine, which makes "quick rejection" decisions by the simplest and fastest math (as in, if the x of the player's 'pointer' position is out of range, reject - then if the y of the player's pointer is out of range, reject - otherwise, test the player's pointer as a candidate selection, probably by distance (or distance squared)). Your challenge will be to efficiently create a database of the pieces to be searched which itself isn't the major performance burden (C# isn't nearly as fast at storage than C++).


For what you describe it is doubtful you'd have a performance oriented concern. PhysX (and thus Unity) is well designed for performance of the collision system. However, you may have extreme efficiency concerns targeting modest mobile hardware or some other reason to choose something that is processor efficient. Any solution you implement in C# is going to consume as much or more memory than the physics system, so if you don't use any physics in your game, implementing your own solution is going to be a better logical choice than if your game pieces also use physics (in which case you'd be creating, essentially, a duplicate/parallel system). It will be worth experimenting to get a "feel" for this sort of thing.

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 dan5071 · Nov 10, 2018 at 03:02 AM 0
Share

Very informative, thanks! I'll be sure to look more into PhysX. In the meantime, I think I'll just stick to limiting the number of OnCollisionEnter() methods I implement by letting the player handle collisions with pickups, since my understanding is that this function would be perfor$$anonymous$$g some calculations each time they collide with something else regardless of whether they are actually supposed to do anything or not.

avatar image JVene dan5071 · Nov 10, 2018 at 05:47 AM 0
Share

Yes, under the hood, PhysX will calculate and therefore process every potential collision in the configuration (they can be limited to layers, for example). However, OnCollisionEnter (and the related functions) are merely responders, which is to say PhysX will call such a responder on every collision, obviating the call if such an override isn't in the user class (so the workload is still performed whether OnCollision... is used or not - only the call itself is 'saved' when you don't provide them).

avatar image
1

Answer by thunderbuns · Nov 10, 2018 at 01:39 AM

I don't know exactly how OnCollisionEnter works. However, I would recommend having the collision detection take place in the player, rather than on each individual item.

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 dan5071 · Nov 10, 2018 at 03:03 AM 0
Share

Yeah, that seems to make the most sense to me too from a performance standpoint even if it doesn't make a huge difference with the amount of pickups generated at a time. Thanks for taking the time to answer!

avatar image thunderbuns dan5071 · Nov 10, 2018 at 03:06 AM 0
Share

You give some answers you get some answers.

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

164 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

Related Questions

Collision Killing CPU/FPS/Performance - Suggestions?! 0 Answers

Make a sprite stop after a collision. 1 Answer

my thing keeps going through walls, but ridged bodies can touch the collider 0 Answers

Inconsistent Particle Collision detection 0 Answers

Multiple Colliders On A Single Object Detecting Each Collider In OnTriggerEnter() 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