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 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 things such as changing values within 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 this code:

 hit.rigidbody.AddForceAtPosition (hit.transform.position, transform.position * hitForce);

Am I doing this 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 everything physics related. You might not notice anything 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 this way:

 hit.rigidbody.AddForceAtPosition(force, hit.point);

Where force is the force you want to apply.

UPDATE

To execute specific code in Update and do something in FixedUpdate you could use a coroutine like this:

 void Update() {
   // your code
   StartCoroutine(applyForce(hit, force));
 }
 
 IEnumerator applyForce(RaycastHit hit, Vector3 force) {
   yield return new WaitForFixedUpdate();
   hit.rigidbody.AddForceAtPosition(force, hit.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.

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

What's FixedUpdate 2 Answers

How do I fix jitter? 1 Answer

Rigidbody character and Update() function 1 Answer

Game not running properly on slow machines (low fps) 1 Answer

Is it okay to use ForceMode.VelocityChange in 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