Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
0
Question by mygoddess007700 · Aug 11, 2021 at 04:07 AM · ridigbody

The sleep function of the rigidbody

I'm working on a project where a rigid ball is hitting multiple rigid castles made of small rigid bodies. But I had a problem with sleeping and waking up rigid bodies.


Here is my problem:

  • My rigid-body sleep function only works for the first object that is instantiated. For later instantiated objects, the sleeping rigidbody will still move.


Here are my codes:

  • This code is placed on each of the little rigid bodies that make up the castle

       private void Start()
         {
             Rigidbody rb = this.gameObject.GetComponent<Rigidbody>();
             if (rb != null)
                 rb.Sleep();
         }
    
    

  • This code is placed on Main Camera

       void StartLevel()
         {
             //如果城堡已经存在,则清除原有的城堡
             if(castle != null)
             {
                 Destroy(castle);
             }
     
             //清除原有的弹丸
             GameObject[] gos = GameObject.FindGameObjectsWithTag("Projectile");
             foreach(GameObject pTemp in gos)
             {
                 Destroy(pTemp);
             }
     
             //实例化新城堡
             castle = Instantiate<GameObject>(castles[level]);
             castle.transform.position = castlePos;
             shotsTaken = 0;
         }
    
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

1 Reply

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

Answer by FlaSh-G · Aug 11, 2021 at 06:58 AM

There is a complicated issue at work here. My guess is that it's related to how Unity handles transform updates. When you do

 transform.position = something;

Unity doesn't immediately update the entire object (like informing the colliders about the updated position). Instead, all transforms that have been changed in a frame will be collected, and their updates will be handled at the end of the frame. This way, if you change the position of an object twice in a row, or the position and the rotation, Unity won't be recalculating everything twice, but only once.

What also happens at the end of a frame is Start, where you send your Rigidbody to sleep. It seems that Start comes first, and the transform updates after, meaning that the Rigidbodies are moved after they're send to sleep. My guess is that that wakes them up again.

There is a simple fix that worked for me while reproducing this: Set the position in the Instantiate call. Instead of

 castle = Instantiate<GameObject>(castles[level]);
 castle.transform.position = castlePos;

do

 castle = Instantiate<GameObject>(castles[level], castlePos, Quaternion.identity);

This will apply the position right away, so the Sleep() call will definitely come afterwards.

Also, you can omit the <GameObject> part:

 castle = Instantiate(castles[level], castlePos, Quaternion.identity);
Comment
Add comment · Show 1 · 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 mygoddess007700 · Aug 12, 2021 at 03:18 AM 0
Share

It is OK. I'm deeply moved. You really made my day.

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

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

Related Questions

Hammer Swing - Joint Break Force and Hammer Object 1 Answer

Disabling a rigidbody 2 Answers

Direction of rigidbody.velocity.magnitude 2 Answers

change the forward, or maybe normalize forward, depending on rotation 1 Answer

Controlling a moving non-kinematic rigidbody along a path 0 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