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 calbar · Aug 27, 2013 at 04:19 AM · rigidbodysleep

Get Rigidbodies to Sleep Faster

I'm colorizing all the rigidbodies that are awake in my scene so it's easier to tell when they go to sleep, and currently, they're taking too long. I can afford to be extremely lenient with accuracy, so I don't mind if they stop simulating before fully settled.

Unfortunately, no amount of tweaking Sleep Velocity and Sleep Angular Velocity seems to work. I've incrementally cranked them up to 10000 from the default 0.14, and also tested 0.014 just to make sure I didn't misunderstand how those values are supposed to work.

I've also verified that the active rigidbodies are indeed getting set with the right sleep velocities.

As a final note, they seem to be deactivating in large groups/piles, which makes sense that the simulation would be recursive. I'd still like the piles to deactivate faster though.

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 robertbu · Aug 27, 2013 at 04:24 AM 0
Share

Untested idea: Why not put them to sleep yourself. Add this line in Update() or FixedUpdate() on a script attached to these objects:

 if (rigidbody.velocity.magnitude < someValue && rigidbody.angularVelocity.magnitude < someOtherValue)
   rigidbody.Sleep();

Note angularVelocity is in radians.

To help them stay asleep, make sure that Sleep Angular Velocity and Sleep Velocity is set above the values you use in the 'if'. Plus you could set the rigidbody.velocity and rigidbody.angularVeloicty to Vector3.zero.

avatar image calbar · Aug 27, 2013 at 05:02 AM 0
Share

Hey, thanks man. Ironically, I just finished doing that, heh! Had to include a $$anonymous$$imum alive time too, otherwise they'd instantly go to sleep once activated. Also of note, for anyone else trying this, the docs say Vector3.sqr$$anonymous$$agnitude is faster than Vector3.magnitude because it avoids the expensive sqrt step, but still works perfectly fine for this application if you adjust the target velocity to compensate.

So, admittedly, it's working exactly how I'd LI$$anonymous$$E it to work through the built-in sleep functionality, but now I worry about doubled efforts, since I assume the internal sleep velocity calculations are still running... correct me if I'm wrong.

Also, I'd imagine the internal sleep checking to be more efficient than what I just whipped up, but that's also just a guess.

So yeah, this is definitely an option, but I wonder if I could squeeze more performance out of just wrangling the existing sleep functionality?

avatar image clunk47 · Aug 27, 2013 at 04:36 PM 0
Share

Try checking if the rigidbody.velocity is greater or equal to $$anonymous$$ / max on any / all axes. Here's an example that will kind of exaggerate what I'm saying here, just to show you how it will work, you can adjust things to be a bit closer to "settled" for more realistic results. This will catch the negative Y axis of a falling rigidbody cube.

 //Example.cs
 
   
 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour 
 {
     Color sleepColor;
     Color awakeColor;
     float $$anonymous$$Y;
     Vector3 cur;
     
     void Awake()
     {
         sleepColor = Color.red;
         awakeColor = Color.green;
         $$anonymous$$Y = -10f;
     }
     
     void Update()
     {
         cur = rigidbody.velocity;
         
         if(cur.y <= $$anonymous$$Y)
         {
             renderer.material.color = sleepColor;    
             rigidbody.Sleep();
         }
         
         if(rigidbody.IsSleeping())
             renderer.material.color = sleepColor;
         else
             renderer.material.color = awakeColor;
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by calbar · Sep 02, 2013 at 02:10 AM

Whelp, I was hoping to leverage the built-in sleep functionality, but I guess the manual approach is more flexible and easier to control.

This is my test for if the rigidbody is ready to go to sleep, as mentioned in my reply to robertbu:

if (grounded && aliveTime >= minimumAliveTime && rigidbody.velocity.sqrMagnitude < sleepVelocity)

3 important notes:

  1. If you don't test whether the rigidbody is grounded (that is, colliding with something else) it's very common for a rigidbody flying near-vertical to freeze in mid-air, where velocity can come very close to 0 at the peak of its arc.

  2. If you want a rigidbody to instantiate without an initial velocity (i.e. to let gravity naturally pull it down) you'll need to impose a minimum alive time before testing its velocity, otherwise it'll instantly go to sleep before it ever picks up enough speed to exceed the sleep threshold.

  3. Use velocity.sqrMagnitude because it avoids the expensive sqrt step of velocity.magnitude, but still works perfectly fine for this situation if you adjust the sleep velocity.

To test whether it's grounded, just use the OnCollision methods Unity provides:

void OnCollisionEnter () { grounded = true; }

void OnCollisionStay () { grounded = true; }

void OnCollisionExit () { grounded = false; }

There's a chance with the above code that a rigidbody could hit a ceiling and freeze from satisfying the OnCollision state and velocity being less than the sleep threshold if it hits head-on. You might have to get a little fancier with your tests if this is a possibility in your game.

Doing the alive time is pretty straightforward:

public void Activate () { if (!rigidbody) { aliveTime = 0f; } }

void FixedUpdate () { if (rigidbody) { aliveTime += Time.deltaTime; } }

I've found a minimum alive time of 1 second and a sleep velocity of 0.1 work well for aggressively putting rigidbodies to sleep without sacrificing a lot of quality.

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

16 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

Related Questions

Why are my Rigidbodies not sleeping? 0 Answers

How do I change force of gravity for a single object? 4 Answers

Deactivate Rigibodies 1 Answer

Go to sleep. 0 Answers

Wakeup Rigidbody Physics on pulling the floor down 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