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 Lautaro-Arino · Nov 02, 2013 at 10:03 AM · function update

Rigidbody AddForce in Update

Can the Rigidbody.AddForce be added in a method called inside the Update() or does it have to be called in FixedUpdate()?

What are the consequence of calling it in a normal Update()?

Also, ive read there should not be a rigidbody inside a hierarchy of an object that itself has a rigidbody. Is this true? What happens if a rigidbody contains a rigidbody?

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

2 Replies

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

Answer by meat5000 · Nov 02, 2013 at 10:18 AM

Physics should be handled in FixedUpdate().

There are perhaps a few exceptions, like making a one off call to ForceMode.Impulse type AddForce or making a one-off velocity change.

The effect otherwise will be inaccurate physics and missed collisions.

Comment
Add comment · Show 6 · 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 Lautaro-Arino · Nov 02, 2013 at 10:31 AM 0
Share

So any check that could result in calling AddForce should not start in update but in fixedUpdate? Or i guess i could handle it like setting a flag so that it will be triggered next fixed update?

avatar image meat5000 ♦ · Nov 02, 2013 at 10:43 AM 0
Share

First statement is correct. Second statement will result in glitches and hangs unless you are very careful. Waiting for an Update() frame to finish before calling a routine in FixedUpdate is pretty much the same as calling the physics from Update(), you are introducing a time delay which will make physics simulation very uneven.

Update() frametime can vary wildly but FixedUpdate() executes with a Fixed time step.

avatar image Lautaro-Arino · Nov 02, 2013 at 10:47 AM 0
Share

Thank you. That is good information. I checked my code and i have an event that is being raised on a collision. And in the event handler it may result in an AddForce. So then its not handled in the FixedUpdate(). How should i do it?

avatar image meat5000 ♦ · Nov 02, 2013 at 10:49 AM 0
Share

If the AddForce is the result of OnCollisionEnter routine, handling it in that routine is just fine as it will only be called that one time and happens at the time, rather than having to wait for an unknown frametime.

avatar image Lautaro-Arino · Nov 02, 2013 at 10:51 AM 0
Share

Great thanks! I will make sure it is like that. Do you know of any reason we should not have objects with rigidbodies inside other objects with rigidbodies?

Show more comments
avatar image
1

Answer by mkgame · Oct 01, 2017 at 11:02 AM

If you start a coroutine, which is waiting for FixedUpdate, then the force will be applied in the next FixedUpdate. I guess, this is what you looking for.

 StartCoroutine( applyForceAtPosition(RootObjectRB, transform.up * RecoilForce, transform.position, ForceMode.VelocityChange) );

 protected IEnumerator applyForceAtPosition(Rigidbody rb, Vector3 force, Vector3 pos, ForceMode forceMode) {
             yield return new WaitForFixedUpdate();
             rb.AddForceAtPosition(force, pos, forceMode);
             yield break;
         }
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

17 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

Related Questions

Setting Quaternion , are Euler angles updated in the same update function? 2 Answers

Unity Mouse Position Raycasting from Point 0 Answers

Issues using GetComponent 1 Answer

BCE0023 Error 1 Answer

How to Instantiate prefabs with function Update? 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