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 HPx · Dec 22, 2011 at 09:01 AM · oncollisionstay

OnCollisionStay perfomance

Hi,

I have a large scene with many collisions and OnCollisionStay is consuming too much resources. I'd like this event to be fired every seconds. Is somebody have a tip for this? I have tried "yield return new WaitForSeconds(1.0f);" but it doesn't prevent from firing.

Is there another way to get existing collision information without OnCollisionStay?

Thanks Harold

EDITED: Thanks for your answer.

But the problem is that my collision is changing each frame and I need more than the existence of the contact.

 Vector3 GetContactsBarycenter(ContactPoint[] points, GameObject target)
 {
     Vector3 bar = Vector3.zero;
     foreach (ContactPoint pt in points)
         bar += pt.point;
     return target.transform.InverseTransformPoint(bar / (float)points.Length);
 }
 IEnumerator OnCollisionStay(Collision collision)
 {
     Debug.DrawRay(collision.gameObject.transform.TransformPoint(
             GetContactsBarycenter(collision.contacts, collision.gameObject)),
             collision.gameObject.transform.TransformDirection(Vector3.up),
             Color.green);
     yield return 0;
 }
Comment
Add comment · Show 1
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 aldonaletto · Dec 22, 2011 at 11:10 AM 1
Share

@HPx, I edited your question and added the script to it (the Your answer box must be used only to post answers - UA is different from forums)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Dec 22, 2011 at 10:06 AM

Why do you need to use OnCollisionStay? Saving a copy of contacts during OnCollisionEnter could be enough, depending on what you need to do:

var myContacts: ContactPoint[];

function OnCollisionEnter(coll: Collision){ myContacts = coll.contacts; } You should edit your question an post your script - we could check it and have more ideas on how to reduce the CPU load.

EDITED:

OnCollisionStay is called each physics cycle while the rigidbody is awake - even if you disable the script - but maybe you could reduce the load by calling the barycenter calculation routine only at some interval, like this:

public float baryInterval = 1; // report barycenter each second float baryTime = 0;

Vector3 GetContactsBarycenter(ContactPoint[] points, GameObject target) { Vector3 bar = Vector3.zero; foreach (ContactPoint pt in points) bar += pt.point; return target.transform.InverseTransformPoint(bar / (float)points.Length); }

void OnCollisionStay(Collision collision) { if (Time.time > baryTime){ // report only when the time has come baryTime = Time.time + baryInterval; // calculate new baryTime Debug.DrawRay(collision.gameObject.transform.TransformPoint( GetContactsBarycenter(collision.contacts, collision.gameObject)), collision.gameObject.transform.TransformDirection(Vector3.up), Color.green); } } Physx will still have to calculate the contact points each time, but at least your code will only execute at the defined interval.

Comment
Add comment · Show 3 · 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 HPx · Dec 22, 2011 at 11:42 AM 0
Share

I don't think my code influence anything.

What is time-consu$$anonymous$$g is the computing of Collision structure. What's why I'd like to execute it once a second or if it's not possible to store collisions in a list with Enter and Exit and calculate Collision struct in a co-routine, but I'm not sure it's possible.

avatar image HPx · Dec 22, 2011 at 01:44 PM 0
Share

I think it's rather a problem of physX than Unity. I think I've reached the joint number limitations. It would be better if PhysX was multithreaded.

Thanks anyway.

avatar image aldonaletto · Dec 22, 2011 at 02:58 PM 0
Share

Physx and Unity functions run much faster than our code: they are created in C++ or other high level languages and compiled to native machine code, while our scripts are compiled to CIL, an interpreted language that has a lot of overhead. Generating the Collision structure takes time, for sure, but maybe reducing the script part you get some appreciable speed improvement. I once had a script that used Debug.DrawRay to show the contact normals of 8 blocks, and it reduced the frame rate to ridiculous levels; when I removed the Debug instructions, the frame rate returned to the regular speed.

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

how to detect collisions between objects if they're sleeping 1 Answer

Jumping script OnCollisionStay didn't work 1 Answer

Instantiate only One Object 1 Answer

OnCollisionStay2D not working in the way I would like. 1 Answer

OnCollisionStay melee 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