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
1
Question by Joel_Shaji · Jul 17, 2020 at 08:56 PM · physicsupdateaddforcefixedupdatetime.deltatime

Does AddForce use Time.deltaTime or TIme.fixedDeltaTime or it depends on whether it is being used in Update() or fixedUpdate()?

I recently learnt how AddForce works. All was clear as the explanations were in terms of how the force is applied each frame and how Time.deltaTime is used to smoothen the force out to 1 second.

But then, I learnt that AddForce is a function of RigidBody and the physics engine handles forces on RigidBodies, so it is better to use AddForce function inside a FixedUpdate() and not an Update().

So how does it make sense to use Time.deltaTime inside a fixedUpdate? it would make sense if it was used inside an update and similarly, fixedDeltaTime was used inside fixedUpdate.

The documentation is pretty crappy about this and I learnt that is to keep the mathematics out of it to keep it simple. But then how do we know how it works?

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
1
Best Answer

Answer by Bunny83 · Jul 18, 2020 at 01:57 AM

Well it works pretty much as I have explained it over here. Though forces applied outside of FixedUpdate will only have an effect once FixedUpdate (or to be more precise once a physics update has happened) ran. So in my answer I said that the AddForce call is equal to the direct velocity change which is somewhat true for the overall effect. However when you call AddForce the velocity is not affected immediately. Forces are accumulated and applied during a physics update. Though conceptionally it makes no difference.


So yes, ForceMode.Force will actually use fixedDeltaTime internally. That's why using that force mode ouside of FixedUpdate doesn't make much sense.


About using AddForce outside FixedUpdate: It all boils down how it's used and which ForceMode. Any continuous forces have to be applied in FixedUpdate. However any one-time-event based things can be applied from whereever you like. Though you would never use ForceMode.Force in that case, especially since it wouldn't be a force in this case but an impulse.


Like Edy said you never use Time.deltaTime or fixedDeltaTime manually when using AddForce. In addition keep in mind that you generally shouldn't use fixedDeltaTime anywhere. Just use Time.deltaTime everywhere you actually need it. Time.deltaTime will actually return a different value depending on from where it's called. It usually returns the latest delta time. However when used inside FixedUpdate Time.deltaTime will return the value of fixedDeltaTime. This makes it easier to move code between Update and FixedUpdate.


Note that you shouldn't really change the fixedDeltaTime setting. We have the FixedUpdate to have a consistent update because many physics machanics can't be linearly scaled by deltaTime. Changing fixedDeltaTime will actually slightly change the behaviour of the physics. I explained that over here

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 Joel_Shaji · Jul 19, 2020 at 07:09 PM 0
Share

What do you mean by not changing fixedDeltaTime setting? Is that the fixed timestep settings of the physics engine? Or dynamically changing it through script during runtime?

avatar image
2

Answer by Edy · Jul 18, 2020 at 12:02 AM

Rules to use Rigidbody.AddForce:

  • Always from FixedUpdate.

  • Never multiply the force value by Time.deltaTime or fixedDeltaTime

A force is a time-independent magnitude. It's integrated along time internally by the physics engine to update the pose of the Rigidbody. Using the above rules guarantees your simulation to behave the same independently of the physics update rate (fixedDeltaTime) or the visual frame rate (deltaTime).

Comment
Add comment · Show 2 · 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 ZatBlure · Aug 10, 2021 at 12:34 AM 0
Share

what if im using Rigidbody.AddForce in a function, and call it when certain key is pressed, does it act the same as FixedUpdate?

avatar image Edy ZatBlure · Aug 10, 2021 at 07:20 AM 0
Share

Any Rigidbody.AddForce() calls outside FixedUpdate() will be accumulated and all them will have effect the next FixedUpdate() cycle. However, note that multiple Update() calls may happen between each FixedUpdate() call. For example, if the key is detected as pressed during two consecutive Update() calls the result will be equivalent to applying twice the original force.

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

138 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 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

FixedUpdate vs. Update for instantaneous ForceMode's 1 Answer

Should we use Time.deltaTime in FixedUpdate 5 Answers

AddForce in Update 1 Answer

Add force in the OnStateUpdate. 0 Answers

Not registering input when checking input in Update and using input in FixedUpdate 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