• 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 Coreyf716 · Nov 04, 2012 at 06:37 PM · rigidbodyupdatefunctionfixedupdateaddforceatposition

Rigidbody.AddForceAtPosition & functions

My first question has to do with function Update () and function FixedUpdate (). According to Unity docs, function FixedUpdate should be used when applying physics to rigidbodies. I have code that involves using rigidbody physics on a key press but it also includes other t$$anonymous$$ngs such as changing values wit$$anonymous$$n the same if statement. Update is called before every frame is rendered. These values may change every frame. Can FixedUpdate be used for changing values every frame?

Also, what would be the result of putting rigidbody physics in Update?

My second questions about rigidbody.AddForceAtPosition. For adding a force on RaycastHit, I used t$$anonymous$$s code:

 $$anonymous$$t.rigidbody.AddForceAtPosition ($$anonymous$$t.transform.position, transform.position * $$anonymous$$tForce);

Am I doing t$$anonymous$$s right?

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 valyard · Nov 04, 2012 at 11:57 PM

First of all you should have split questions. But I'll try to answer both.

All physics calls you must do in FixedUpdate because that's where Unity recalculates everyt$$anonymous$$ng physics related. You might not notice anyt$$anonymous$$ng if calling physics methods in Update but usually behavior wil be wrong. If you see odd behavior check whether you have physics code in FixedUpdate.

And yes, you can set other parameters in FixedUpdate. But it runs with slower framerate than Update so parameters you update in FixedUpdate will not be seen immediately and their discreet values will span across several frames.

The second part. addForceAtPosition has the following signature: function AddForceAtPosition (force : Vector3, position : Vector3, mode : ForceMode = ForceMode.Force) : void. So you need to call it t$$anonymous$$s way:

 $$anonymous$$t.rigidbody.AddForceAtPosition(force, $$anonymous$$t.point);

Where force is the force you want to apply.

UPDATE

To execute specific code in Update and do somet$$anonymous$$ng in FixedUpdate you could use a coroutine like t$$anonymous$$s:

 void Update() {
   // your code
   StartCoroutine(applyForce($$anonymous$$t, force));
 }
 
 IEnumerator applyForce(RaycastHit $$anonymous$$t, Vector3 force) {
   yield return new WaitForFixedUpdate();
   $$anonymous$$t.rigidbody.AddForceAtPosition(force, $$anonymous$$t.point);
 }
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 Coreyf716 · Nov 05, 2012 at 12:48 AM 0
Share

The code for AddForceAtPosition has an error.

avatar image valyard · Nov 05, 2012 at 01:05 AM 0
Share

What error?

avatar image Coreyf716 · Nov 05, 2012 at 01:07 AM 0
Share

No appropriate version of 'UnityEngine.Rigidbody.AddForceAtPosition' for the argument list '(float, UnityEngine.Vector3)' was found.

avatar image valyard · Nov 05, 2012 at 01:11 AM 0
Share

Of course, because force must be a Vector3. Also I updated the post with coroutine example.

avatar image Coreyf716 · Nov 05, 2012 at 02:22 AM 0
Share

var hitForce : float;

hit.rigidbody.AddForceAtPostion (transform.TransformDirection (Vector3.forward) * hitForce, hit.point);

This should work as it has the Vector3, the force, and the point at which the ray hits the rigibody.

Show more comments

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

11 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

Related Questions

Can use FixedUpdate in Update? 1 Answer

Vertical and horizontal look seperated into update and fixed update. 0 Answers

Splitting one event's code between Update and fixedUpdate 2 Answers

What's FixedUpdate 2 Answers

How do I fix jitter? 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