• 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 Divinitize1 · Dec 07, 2019 at 12:11 AM · physicsforce

What's the correct way of doing addforce in fixed update?

I am currently using this to apply a brief force to a rigidbody, It works if the game is running smoothly but if there's a slight drop in framerate or anything the expected force is obviously way higher than expected. I have a bit of unity experience now but haven't really dealt with physics much to know the correct way, hoping someone can kindly help.

     public GameObject go;
     Rigidbody rb;
     public float forceX;
     public float forceY;
     public float forceZ;
     bool applyingForce= false;
 
     void Start()
     {
         rb = go.GetComponent<Rigidbody>();
         StartCoroutine(ApplyForce());
     }
     IEnumerator ApplyForce()
     {
         rb.isKinematic = false;
         applyingForce= true;
         yield return new WaitForSeconds(.5f);
         applyingForce= false;
     }
     void FixedUpdate()
     {
         if(applyingForce)
         {
            rb.AddForce(new Vector3(forceX,forceY,forceZ)); 
         }
     }
Comment
Add comment · Show 1
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 Divinitize1 · Dec 07, 2019 at 04:32 PM 0
Share

Still looking for some advice

2 Replies

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

Answer by lgarczyn · Dec 07, 2019 at 07:12 PM

Coroutines work on the Update frame, and are not precise enough for this kind of frame-sensitive jobs.

Also, using ForceMode.Acceleration frees your result from your physics tickrate and rigidbody weight, and using a Vector3 simplifies your code.

  public Vector3 force;
  float applyingForceTimer = 0f;
 
  void Start()
  {
      rb = go.GetComponent<Rigidbody>();
      applyingForceTimer = 0.5f;
      rb.isKinematic = false;
  }
  
  void FixedUpdate()
  {
      if(applyingForceTimer > 0f)
      {
          rb.AddForce(force, ForceMode.Acceleration);
          applyingForceTimer -= Time.fixedDeltaTime;
      }
  }
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 Brogan89 · Dec 07, 2019 at 07:15 PM 0
Share

this is the correct answer. nice, i can never be bothered typing out full code lol :D

avatar image Divinitize1 · Dec 07, 2019 at 11:01 PM 0
Share

Thanks for the replies guys, I will try this shortly and let you know how it goes.

avatar image
0

Answer by Brogan89 · Dec 07, 2019 at 06:55 PM

Pretty much FixedUpdate() kind of runs in its own thread. So it could be called twice in one update loop cycle.

based on your code above i would just move remove the applyingForce bool and move the line rb.AddForce(new Vector3(forceX,forceY,forceZ)); to immediately under yield return new WaitForSeconds(.5f); then you should be good.

Edit: actually second look based on your code and you wanting to add force for 0.5 seconds? if so, you can do this in the coroutine instead.

 var time = 0.5f;
 while (time > 0)
     AddForce();
     time -= Time.deltaTime;
     yield return null;
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 lgarczyn · Dec 07, 2019 at 07:06 PM 0
Share

There's a bit of misinformation here, FixedUpdate definitely doesn't run on its own thread, but it is a specific part of the game loop.

The problem here is the use of Coroutines, which are not precise enough for this kind of job.

avatar image Brogan89 lgarczyn · Dec 07, 2019 at 07:12 PM 0
Share

yea i know i shouldn't of used that word. is not on a separate thread, but it runs independent of the Update loop thus frame rate. which is why you are experiencing unexpected results when you have frame dips. See https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.FixedUpdate.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

195 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

Related Questions

reading forces applied to a rigidbody 1 Answer

Shooting a cannonball. 6 Answers

Direction of rotation and ApplyForce 1 Answer

2.5d Player Controller 0 Answers

AddRelativeTorque not rotating 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