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
0
Question by Mungoid · Apr 09, 2012 at 09:09 PM · physicsrigidbodies

OnCollisionEnter performance

I have a 6x6x8 (about 288) stack of rigidbody cubes set up on my simple scene and i have a player controlled object that can either run into those cubes, making them topple or you can hit a key and explode causing the cubes to fly.

in the explosion loop in my player code, I paint the cubes red if they were directly in the blast radius and tell those specific cubes they have been hit. In the cube behavior script I have an OnCollisionEnter that checks if they have been hit by the explosion, and if they have, paint them green only if they hit the ground.

The code works fine but it gets really choppy for a few seconds during the initial blast. If i take out the block behavior code, it runs just fine. Im sure there is a more efficient way to do what im doing but I am not sure what it is. The code in question is below.

How can i do the OnCollisionEnter better?

My player code:

 Vector3 explosionPos = myTransform.position;
             Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
             foreach(Collider hit in colliders)
             {
                 if(!hit)
                     continue;
                 
                 if(hit.rigidbody)
                 {
                     hit.rigidbody.AddExplosionForce(explosionPower, explosionPos, explosionRadius, 3.0f);
                     
                     if(hit.collider.gameObject.tag == "Block"){
                         hit.GetComponent<BlockBehavior>().HasBeenHit(true);
                     }
                     
                     hit.renderer.materials[0].color = Color.red;
                 }
         }

My block behavior code snippet:

 void OnCollisionEnter(Collision col){
         if(hasBeenHit){
             if(col.gameObject.name == "Ground"){
                 renderer.materials[0].color = Color.green;
             }
         }
         
     }
     
     public void CanCollect(bool col){
         hasBeenHit = col;    
     }
Comment
Add comment · Show 2
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 Mungoid · Apr 10, 2012 at 12:33 AM 0
Share

So i did get it to run better by pulling the renderer.materials[0].color = Color.green; out of the collision method and putting it in the update, checking the hasBeenHit. I also cached the transform and use the cached one ins$$anonymous$$d. Not sure if this is still the proper way but its running better

avatar image garner · Apr 10, 2012 at 02:19 AM 1
Share

maybe add a really small value yield in if(hit.rigidbody){ }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Apr 10, 2012 at 02:55 AM

Your original code seemed efficient already.
But lots of collision events are reported before the block enters sleep mode, and they could be causing the slowdown. I think that making sure the color is set only once to green could improve the performance - in OnCollisionEnter or Update.
You could use two bool flags to do that:

bool isGreen = false; bool wasGreen = false;

void OnCollisionEnter(Collision col){ if (!isGreen && hasBeenHit && col.gameObject.name == "Ground"){ isGreen = true; } }

void Update(){ if (isGreen != wasGreen){ if (isGreen){ renderer.materials[0].color = Color.green; } wasGreen = isGreen; } }

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is causing this odd physics behaviour? 0 Answers

Change Inertia of rigidbody (Ixy) 1 Answer

How can you carry rigidbodies without them appearing jumpy? 1 Answer

When I do a ragdoll simulation, my character falls on the ground but then the character's joints (their colliders as well) just tear apart. 0 Answers

Add Force without Rotation 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