- Home /
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?
Could you share your code @$$anonymous$$ystical$$anonymous$$rCool
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);
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 :)
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
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.
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?
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
Follow this Question
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