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
0
Question by Bowbowis · Jan 03, 2021 at 02:27 PM · physicsrigidbodyinputupdatefixedupdate

Is it okay to use ForceMode.VelocityChange in Update()?

I'm making a 3D platformer in which the player is a rigidbody controlled primarily through AddForce (velocity, ForceMode.VelocityChange) methods. Since it's important for the character to respond immediately to inputs, especially for things like jumping, where the initial force is applied on the button press but can be cancelled by releasing the button before the jump reaches its apex, I was considering applying the forces in Update(). I've read that one should do physics updates in FixedUpdate (), but I've also seen people say that only really matters for continuous forces. Since ForceMode.Velocity is an instantaneous force, I want to know: is using it in Update() kosher?

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 Llama_w_2Ls · Jan 03, 2021 at 02:42 PM 0
Share

Sure. Since it applies an instant force and ignores mass, doing it in the Update would really have the same effect as doing it in the FixedUpdate, like Force$$anonymous$$ode.Impulse. It just depends if you want/need two methods running every frame or at fixed timesteps, or just one. @Bowbowis

avatar image Bowbowis Llama_w_2Ls · Jan 03, 2021 at 04:15 PM 0
Share

Thanks for the reply. Could you clarify what you meant by that last sentence? @Llama_w_2Ls

avatar image Llama_w_2Ls Bowbowis · Jan 03, 2021 at 08:18 PM 0
Share

Like you could have a FixedUpdate and an Update running in the same script, or just a FixedUpdate, which would be very very slightly better performance-wise and somewhat easier to read, as your code file is smaller. It's just small gritty bits that don't really matter, but would be better to leave out regardless.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Edy · Jan 03, 2021 at 09:03 PM

No, that may cause inconsistencies. Update and FixedUpdate may be considered two different execution loops:

  • Several Update calls may happen between two FixedUpdate calls (Example: 100 Hz screen and/or vsync disabled, while FixedUpdate runs at fixed 50 Hz).
  • Several FixedUpdate calls may happen between two Update calls (Example: temporary frame rate drops below 50 fps because of cpu load).

Calling AddForce from Update won't have any effect until the physics update is reached, that is after the next FixedUpdate occurs. Calling AddForce multiple times consecutively accumulates the effect that will reach the physics update. So calling AddForce from Update may have these effects:

  • If AddForce is called in two Update calls before a FixedUpdate happens, this will double the velocity change applied to your rigidbody. Or even multiplied further, if that happens during multiple consecutive Update cycles. Note that this is a somewhat common glitch in commercial games. If you're used to watch speedruns, this kind of glitch is exploited where possible to gain extra velocity.
  • If several consecutive FixedUpdate calls occur due to frame drops (= Update call drops), you may then miss some user inputs in these situations.

That said, the best solution to handle all user inputs as fast as possible while keeping consistency is:

  1. Detect the user input in both Update and FixedUpdate. Raise some flag if user input is detected.
  2. Check for the flag at the end of FixedUpdate. If it's raised, call AddForce once and clear the flag.

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

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

How do I pass an input in Update() to FixedUpdate() for Physics movement? 2 Answers

Rigidbody character and Update() function 1 Answer

Input and applying physics, Update or FixedUpdate? 3 Answers

Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 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