Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Casiell · Sep 11, 2018 at 05:31 PM · physics2daddforce

Impulse force time

Ok, I couldn't formulate this question simple enough to google it effectively, so I hope I can get some help here.

I have an object that I add force to using Rigidbody2D.AddForce with ForceMode2D.Impulse as a parameter. Now the question is, is it possible to calculate time in which this force will be affecting my velocity if I know starting velocity, mass and everything else I can get from RigidBody2D?

I need to calculate this using only starting parameters as the actual velocity can change by other means as well.

The in-game situation is: I have a character that performs 'dash'. I do this with AddForce and it works great! But I need to know for how long the character is dashing so things can happen in that period of time. Any collisions or actions that happen along the way are irrelevant for me, I only need the time.

Right now I just approximated the time by dividing the force I add (usually around 50) by 100 and it looks quite accurate for small forces, but I don't really like this solution.

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
2
Best Answer

Answer by Zarenityx · Sep 11, 2018 at 09:59 PM

First, the easy (and recommended) way out

Consider making your character have a set dash time (or dash distance) to begin with. Save the initial magnitude of their velocity, for instance, and when the dash is over, set the magnitude of their current velocity back to the previous magnitude or their current, whichever is smaller. This allows the direction to still change freely in-dash.
Furthermore, I highly recommend this method over forces because you maintain more explicit control over how the character dashes. This means that balancing is easier, and it opens the door for easily adding modifiers, different character stats, status effects, etc. It also makes it much more intuitive for you to edit. The physics engine is great for newtonian physics, but most often a character is not subject to these same laws- a dash might be the result of the character kicking off with their feet inhumanly fast, or something much more bizzare, like magic or some sort of sci-fi gadget, and regardless, nothing in real life really dashes the way a game character should. Trying to approximate a dash using forces, and finding the length, is like shooting a gun in space and asking how long before the bullet slows down.

Although, I understand that this is not actually an answer to your question, so if you still want to do what you are doing, it's important to quickly look at:

The physics:

The best way to think about the force modes in my opinion is to think about their units. These are documented with the 3D ForceMode, but the principle is the same in 2D.


-Force is in the units of force: Newtons, or a kg*m/(s^2).
This means that every second that AddForce is called, the velocity increases by force/mass.

-Impulse is force*time, and thus in units of kg*m/s.
This means that every time that AddForce is called, the velocity increases by impulse/mass.


This means that by definition Impulse is instantaneous- the force never starts or stops its effect, it simply applies an immediate change. The explicitly written code for adding an impulse is:

 rigidbody.velocity += impulse/rigidbody.mass;

Calling this over a period of time doesnt make a lot of sense, because rather than adding accelleration per timestep, the velocity is just being changed immediately. Additionally, the smallest timestep- the framerate- changes. Ordinarily, this would be solved with:

 rigidbody.velocity *= (impulse*Time.deltaTime)/rigidbody.mass;

This is, interestingly, the definition of force in the update function. In fixedUpdate, Time.fixedDeltaTime is used, and you end up with the exact same result as ForceMode.Force.


For this reason, the answer for how long an impulse applies is 0. It happens instantly. Now, obviously, this doesn't predict how long your dash will last, so I have amended this to a slightly different question: How long after an impulse will it take for resistive forces to bring the object back to its original velocity, assuming no other forces are applied. The answer relies quite a bit on what kind of force is resisting motion, however this is usually friction. For most characters, air resistance is negligable and frankly often feels bad anyway. So:

The Calculation:

Assuming a constant resistance R, such as friction, the time in seconds it takes to go from the impulse velocity to the starting velocity is I/R, where I is the impulse applied.
To express this in more physical units, the time is I/(u*g*m), where g is gravity, m is mass in kg, and u is the coefficient of friction.


Regardless, this will give results that, while they would work, might feel bizzare. I highly recommend the method I provided in the beginning, as it is easier to make a dash feel right that way. Using the physics engine for things that aren't truly physics never produces the crisp feel that you're going for.

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 Casiell · Sep 12, 2018 at 06:19 AM 0
Share

Wow, that's one detailed answer! Thanks a lot, I will experiment with the solutions you provided, and hopefully I get satisfying result.

One remark tho, in second to last paragraph you said "I/R, where F is the impulse applied". There is no F in the equation, I figure you mean 'I' as 'R' is the resistance?

avatar image Zarenityx Casiell · Sep 14, 2018 at 02:30 PM 0
Share

Yeah, sorry about that. I is the impulse, R is the resistance. I have edited the original answer to correct this error

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

92 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

Related Questions

How long addforce applied? 0 Answers

Is it possible to have slow acceleration and high drag at the same time? 0 Answers

RigidBody2D Performance with AddForce on WakeUp 1 Answer

Rigidbody2d.addforce in the direction of mouse problem 1 Answer

Circular movement using AddForce() 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