Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
1
Question by jwulf · Nov 05, 2016 at 03:11 PM · performancephysics2dcollider2dtriggersdetection

Performance of player detection: Colliders/Triggers vs OverlapCircle

I am working on a 2D side scroller in which most of the enemies are in a patrol-state at the start and then, if the player gets near them, enter a chase- or battle-state. I am currently thinking about different methods to detect if a player is near an enemy:

The first approach is to give every enemy a CircleCollider2D which is a trigger to detect the player. (The Physics2D-Layers are arranged so that only the player would cause OnTriggerEnter2D() to be called.) Then in OnTriggerEnter2D(), the player is registered as found and the chase-state is entered. But it seems uneccessary to me, to check for the player every single frame. Once or twice a second would be enough for me, so I am thinking about doing something like:

  • Creating a method LookForPlayer() which uses Physics2D.OverlapCircle() to detect the player and if the player is found, calls a callback function (so that callback kind of replaces OnTriggerEnter2D()).

  • Make this method being called repeatedly by using something like InvokeRepeating("LookForPlayer", 0.5f, 0.5f).

Is there any performance advantage in my second suggested method (because the physics engine needs to do the detection only twice per second instead of every frame) or would the performance maybe even be worse, becaus InvokeRepeating() causes some overhead and Colliders/Triggers are highly optimized for such cases anyway and thus better than using Physics2D.OverlapCircle() manually?

Maybe someone has experience with that or possibly a great understanding of the engine, so they know about the mentioned possible performance differences? I know I could of course run my own "benchmark" for this, but I wanted to ask for experiences first before doing uneccessary work... ;)

Thanks, Jan

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

Answer by brunocoimbra · Nov 06, 2016 at 12:24 AM

If depends heavily on which is your target platform, the amount of enemies in your scene and such, so yeah, running your own benchmark would be your best option, but from my understanding of Unity's 2D physics and general physics in game:

  1. If you are using Unity's colliders and your objects do move, attach an Rigidbody to that object (or Rigidbody 2D in this case) and let isKinematic checked if you don't need the physics to move. Static triggers don't need them, as they are "baked" in their position.

  2. You can always adjust the Fixed Time Step to adjust how frequent the physics needs to be updated.

  3. Half a second can cause an "lag-feel" in your game depending on what you are doing.

  4. As I mentioned Fixed Time Step, it is good to remember that the physics are not updated "every frame".

  5. With the Rigidbody, you have the option to put that object to "Sleep", so you can improve your game performance later if needed (as enemies that are too far away don't need to be updated anymore.

  6. If your map is too big, you can have multiple scenes in your game and loading them as you needed (take a look around about level streaming if you don't know what I am talking about).

  7. If targeting mobile, level streaming usually is not a good option, as having a huge amount of colliders and calling Physics2D.OverlapCicle() in a huge amount of objects too (having a huge amount of objects using physics at same time is not a good option at all in mobile devices).

  8. If you are using Unity's physics layers, remember that you are already optimizing a lot the whole system, as you are already removing unnecessary OnTriggerEnter calls, while OverlapCircle would be called anyway.

Summing up, the Unity's collider are very good and already provides a lot of optimization options, so I do not see why not to use them. In general, using triggers is the best option as, even if having performance issues, you can always optimize it by yourself, as using the InvokeRepeating to disable objects too far away or to put their rigidbodies to sleep.

Sorry if my answer wasn't clear enough.

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
avatar image
0

Answer by kobi666 · Mar 12, 2021 at 09:47 PM

A very important note here, is that for 2d, unity explicitly requires that you move objects using rigidBody.movePosition and not transformPosition, otherwise it causes a big performance issue (physics engine kicks in for some reason, and the function to get contacts is called frequently)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse click on line - performance question 1 Answer

double collision detection in breakout game 1 Answer

problem solving ghost vertices with trigger and collision colliders 0 Answers

How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer

Performance issues when moving 2D colliders by code. 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