• 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 /
  • Help Room /
avatar image
0
Question by kruemelkeksfan · Jun 16, 2020 at 03:48 PM · physicsrigidbody.addforcerigidbody.velocityforcemode

ForceMode.VelocityChange not behaving as expected

I understood from the Doc, that these 2 lines should work identical:

// Works fine: rigidbody.velocity += velocityChange; // Bugs out and sets Velocity to ridicoulously high, seemingly random Values: rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

However one works and one doesn't. Anybody knows something about this?

Complete Script: https://pastebin.com/0NUSLAQL

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

Answer by Edy · Jun 16, 2020 at 06:22 PM

It's not the same.

  • Rigidbody.velocity overrides the velocity that comes from the physics calculations.

  • AddForce in ForceMode.VelocityChange applies the velocity change during the internal physics calculations. The resulting velocity also accounts for other forces and accelerations (i.e. gravity) applied during the physics step.

Both methods are not equivalent, therefore the results are different.

Comment
Add comment · Show 5 · 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 kruemelkeksfan · Jun 16, 2020 at 11:43 PM 0
Share

@Edy So even though rigidbody.velocity += something only increments the value, the physics update would ignore rigidbody.velocity for this physics frame? I should add, that the velocity-changing code is in the Update() ins$$anonymous$$d of the FixedUpdate() method.

avatar image Edy kruemelkeksfan · Jun 17, 2020 at 08:07 AM 0
Share

All physics-related operations should be performed in FixedUpdate, otherwise the results are dependent on the frame rate. The physics steps uses your velocity, but as far as I can tell the calculation is precise when the velocity is calculated out of all the factors affecting the rigidbody. Internally, each physics step is subdivided in smaller steps (substeps) for a more precise calculation.

avatar image kruemelkeksfan Edy · Jun 17, 2020 at 12:27 PM 0
Share

The framerate dependency can easily be compensated by multiplying the right things with Time.deltaTime. I use the Update() method, because Update() turned out to deliver smoother results when processing user input. The alternative would be to query the user input in Update(), set flag variables for which keys are pressed and process the key presses in FixedUpdate(). But I prefer multiplying by Time.deltaTime ins$$anonymous$$d of making 5-10 new variables (or an array).

To give a little more context of what I am trying to do:

 Vector3 direction = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical"));
 float speed = movementSpeed;
 [... apply some modifications to speed ...]
 Vector3 velocityChange = ((direction * speed) - rigidbody.velocity);
 rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);

AT$$anonymous$$ this code should set the velocity to the direction deter$$anonymous$$ed by user input. This should have the same effect as rigidbody.velocity = direction * speed and therefore indeed override all other physics. The reason I am doing this is, that I want to have this simple step working, before moving on to something like this:

 Vector3 velocityChange = ((direction.normalized * 0.1f * Time.deltaTime) - rigidbody.velocity);
 rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);

With that code I should get a linear acceleration ins$$anonymous$$d of instant setting of a target velocity and other physics forces should work fine with that. However I don't want to go that far, as long as the basic instant setting of the velocity is not working properly.

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

285 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

my 2d character isnt jumping with constant velocity on applying force 1 Answer

Accelerate Player with Rigidbody.AddForce 0 Answers

Rigid body robot animation 0 Answers

Using both RB Velocity and AddForce? 1 Answer

Hey I am trying to calculate velocity of a gameObject without using rigidbody 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