• 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
0
Question by Tialis · Aug 16, 2014 at 02:19 PM · 2dphisics

Performance on scene with many colliders

Hi all,

I followed "Angry birds"-like 2D tutorial and decided to go a bit further, with learning purposes. I have a couple of questions I want to understand on colliding objects (particularly in 2D).

In the tutorial, objects cannot be "broken", so I decided to set a "health" script to every game collider, so they can be broken. But one question appeared:

  • Does every object need to have a OnCollision2D() to calculate the loss in health between different objects? Wouldn't that be terribly un-efficient?

I mean... if we have 3 colliders, we should check if 1 collides with 2 or 3, if 2 collides with 3, and that's all... What would be the correct way to program it? One script that handles all colliders between each others?

Also another question...

  • Which would be the correct physical variable that would be proportional to "damage"? Force between objects? Momentum?

Does anyone know what do they actually use in Angry Birds?

Thank you all!

Comment
Add comment · Show 3
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 meat5000 ♦ · Aug 15, 2014 at 08:17 PM 0
Share

Have each object handle it own collision side effects.

Under physics, you do not need to code anything for normal collisions to occur. I say side-effects as the OnCollision routines are where you code anything else you want to happen, like destroying an object.

There is no 'correct' when it comes to your own game logic.

avatar image _dns_ · Aug 16, 2014 at 03:39 PM 0
Share

Hi, for the first question about efficiency: Unity handles all those collisions with dedicated physics engine optimizations, contact points lists etc... This is the code you describe to "check collisions between 1 & 2, 2 & 3 etc". Search for physics engine "broad phase" optimization if you want to know more about the subject. When everything is solved, it calls the OnCollisionEnter/Exit/Stay only if those scripts are present.

For the second question: computing damage or launching sound when there is a collision is always tricky. There is no easy solution that works for all games. I would compute damage using the velocity magnitude and direction of both objects, taking mass into account, maybe angular velocity too. You will need to try $$anonymous$$imum and maximum values, use dot product to compare directions etc... One approach would be to test the change of velocity and angular velocity the engine did from one frame to another: example: you store the velocity during one FixedUpdate, then on next FixedUpdate, you compare the new velocity with the previous stored one. If the difference is important, it means that the physics engine wants to move your object a lot = it has been hit hard. This kind of script usually takes lots of testing and iteration until it "feels right". It can be long but very interesting to do and to learn more about physics engine and gameplay program$$anonymous$$g.

avatar image Tialis · Oct 30, 2014 at 09:40 PM 0
Share

Thank you both for your answers! They were helpful! :)

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Move a tile (sprite) with drag and drop in Unity 2D 0 Answers

2D Animation does not start 1 Answer

Move 2D sprite to multiple destinations similar to animation 1 Answer

Should build of a 2D project using the new 2D features in 4.3 be smaller than a 3D project? 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