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 MysticalMrCool · Dec 30, 2020 at 12:51 PM · rigidbody2dgravityplatformerrigidbody physics

How to instantly accelerate a falling game object to the maximum fall speed?

Hey everyone. Currently making my first Unity2D game and I'm a bit stuck. I am creating a falling platformer and when the game starts I want the player game object, which has a Rigidbody2D attached, to already be falling at max speed, rather than spawn and accelerate toward the maximum fall speed.

This would only be for when the game starts, after that I will want the player game object to accelerate toward maximum fall speed when falling. What would be the best way to go about this?

Comment
Add comment · Show 1
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 prakyathd801 · Dec 30, 2020 at 12:52 PM 0
Share

Could you share your code @$$anonymous$$ystical$$anonymous$$rCool

2 Replies

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

Answer by Eno-Khaon · Dec 30, 2020 at 11:12 PM

As @sacredgeometry mentions, your potential terminal velocity is infinity if there's no drag to restrict vertical speed. If there *is* drag, then you can derive a maximum speed by determining the value which returns itself by the following principles:

 // https://forum.unity.com/threads/understanding-rudimentary-unity-physics.397547/
 // Box2D drag application, specifically (PhysX applies drag differently)
 new velocity = currentVelocity + (gravityScale * gravity * Time.fixedDeltaTime)
 new velocity *= 1.0f / (1.0f + Time.fixedDeltaTime * drag)

 // Using Y-axis for simplicity, but that wouldn't account for non-axis-aligned gravity as-is
 if([new velocity].y == [current velocity].y)
 {
     // Terminal velocity reached
 }


To determine that, you need to figure out how much of an effect gravity has per-frame, then find that point where the drag calculation negates that same value:

 per-frame gravity = gravity * Time.fixedDeltaTime; // 0.1962 using default gravity and fixedDeltaTime (9.81 and 0.02 (50 fps) respectively)
 per-frame drag base = Time.fixedDeltaTime * drag; // Example: 0.002 with 0.1 drag and default fixedDeltaTime
 drag multiplier = 1.0f / (1.0f + [per-frame drag base]); // Example: ~0.998 (slightly greater)
 terminal velocity = [per-frame gravity] / [per-frame drag base];
 // Example: 0.1962 / 0.002 = 98.1
 verification = ([terminal velocity] + [per-frame gravity]) * [drag multiplier] = [terminal velocity];
 // (98.1 + [per-frame gravity]) * [drag multiplier] = 98.1


On this basis, if drag = 0, then you'd divide by 0, resulting (in this context) in infinitely high terminal velocity.


Regardless of whether you decide to assign drag, however, to start at an increased falling speed, just assign a velocity to the character immediately.
 // Simple example
 
 // When game starts
 playerRigidbody2D.velocity = new Vector2(0f, -50f);
Comment
Add comment · Show 3 · 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 MysticalMrCool · Dec 31, 2020 at 04:19 AM 0
Share

Thank you for your very detailed answer. I ended up increasing the gravity scale and drag for the moment the game starts then back down to normal once gameplay begins. Ended up being completely seamless so I'm happy with it :)

avatar image Eno-Khaon MysticalMrCool · Dec 31, 2020 at 11:13 AM 0
Share

As an additional note, because both the gravity and drag are applied per-frame, FixedDeltaTime can be eli$$anonymous$$ated from the calculation of ter$$anonymous$$al velocity, as it applies equally to each:

 // Following the same example values in my answer:
 [ter$$anonymous$$al velocity] = [vertical speed] / drag
 98.1 = 9.81 / 0.1
avatar image Bunny83 · Dec 31, 2020 at 11:28 AM 0
Share

Note that I created some helper functions over here to calculate the ter$$anonymous$$al velocity for a given drag and constant acceleration as well as a function to calculate the required drag for a given constant acceleration and a desired ter$$anonymous$$al velocity. There's also a method for the third case to calculate the required acceleration to reach / maintain a certain ter$$anonymous$$al velocity for a given drag value.

edit
I just realised that this question was about Physics 2d which works different. So my answer doesn't apply here. Only when you use 3d physics.

avatar image
0

Answer by sacredgeometry · Dec 30, 2020 at 06:57 PM

Increase the gravity? Gravity is outlined as as a rate of acceleration i.e. 9.8 meters per second squared i.e. 9.8 meters per second per second.


If you want to speed up (or remove) the process of reaching terminal velocity then gravity essentially needs to be infinite or you need to turn it off and roll your own.

It will feel really really wrong to have things move that way because things will fall at different rates.


It sounds like you are trying to solve another problem though. So what is that problem?

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 MysticalMrCool · Dec 30, 2020 at 09:51 PM 0
Share

Thanks for the response! Unfortunately, if I were to increase the gravityScale that would also increase the maximum fall speed. All I want is for the player game object to appear as though they were already falling when the game starts, rather than beginning to fall when the game starts.

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

125 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

Related Questions

Rigidbody2D character movement problem 0 Answers

2D Box Colliders Overlapping On Collision 4 Answers

using rigidbodies for platforming having issues 1 Answer

How to use Automass? 1 Answer

Do rigid bodies with force add force to other objects? 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