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?
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);
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?