• 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
2
Question by Andrige_ · Nov 13, 2013 at 09:27 PM · rigidbodyforcemode2d character

Rigidbody2D and ForceMode.AddVelocity?

The new 2D update (Unity v4.3, released 2013-11-12) have come with the new Rigidbody2D component.

My previous code uses the 3D-based rigidbody, moving it using Rigidbody.AddForce(). I specified that the AddForce() uses ForceMode.VelocityChange, which conveniently ignores mass and just makes it better for a playable character.

Rigidbody2D.AddForce() on the other hand does not support ForceMode at all, it has only one way of dealing with velocity and that is 'force = mass * acceleration'.

Here's the part of my code that applies the target velocity to the rigidbody:

             /* Apply motion */
             Vector3 _velocity = myRigidbody.velocity;
             Vector3 _velocityChange = (TargetVelocity - _velocity);
 
             /* Move rigidbody */
             myRigidbody.AddForce(_velocityChange,ForceMode.VelocityChange);


How to convert this movement to the Rigidbody2D? Preferably I am looking for an identical conversion, as I am quite keen on how the character feels right now when moving.

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

3 Replies

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

Answer by s_guy · Nov 14, 2013 at 02:10 AM

Yeah, no forcemodes (yet?) on Rigidbody2D. However, you should be able to the same result as ForceMode.VelocityChange by just adding your new velocity to the rigidbody2D.velocity.

Comment
Add comment · Show 1 · 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 Andrige_ · Nov 14, 2013 at 01:00 PM 0
Share

Thank you! That works just as I had hoped.

avatar image
7

Answer by Kencho · Dec 05, 2013 at 01:54 AM

Here are some extension methods for Physics2D to add this missing functionality to AddForce, strongly based on the solution by gfoot at this thread.

 public static class Physics2DExtensions {
     public static void AddForce (this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode = ForceMode.Force) {
         switch (mode) {
         case ForceMode.Force:
             rigidbody2D.AddForce (force);
             break;
         case ForceMode.Impulse:
             rigidbody2D.AddForce (force / Time.fixedDeltaTime);
             break;
         case ForceMode.Acceleration:
             rigidbody2D.AddForce (force * rigidbody2D.mass);
             break;
         case ForceMode.VelocityChange:
             rigidbody2D.AddForce (force * rigidbody2D.mass / Time.fixedDeltaTime);
             break;
         }
     }
     
     public static void AddForce (this Rigidbody2D rigidbody2D, float x, float y, ForceMode mode = ForceMode.Force) {
         rigidbody2D.AddForce (new Vector2 (x, y), mode);
     }
 }


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

Answer by slake_it · Mar 02, 2016 at 10:00 AM

Force mode has been added 2d rigidbodies

Comment
Add comment · Show 1 · 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 XenoRo · Jun 30, 2017 at 08:18 PM 0
Share

As of version 5.61f1, still only half-implemented, and missing the mass-ignoring modes (Acceleration and VelocityChange)... Sincerely, can't see why unity doesn't use the good old Force$$anonymous$$ode type for both 3D and 2D.

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

20 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

Related Questions

Rigidbody enemy Forcemode.VelocityChange 1 Answer

Difference and uses of rigidbody force modes ? 2 Answers

Trying to Understand Rigidbody ForceMode Derivation 3 Answers

Confusion with AddForce with ForceMode.Acceleration 1 Answer

Unable to Apply External Forces to my Rigidbody 0 Answers


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