- Home /
Use Rigidbody2D's Drag to slow down flying character
I have a character that gets launched into the air with rb.AddForce(direction * punch_strength_value * punch_strength_total);
I want this character to be slowed down the higher he is in the air, creating a "soft-limit" so the character does not go too far up. I figured this could be done by changing the Rigidbodies drag according to the character's height, but I was wrong:
public float height_slowdown = 1;
void Update()
{
heigth = transform.position.y;
rb.drag = height * height_slowdown;
}
Using this, the character launches into the air and is then slowed down very abruptly as if there was an invisible wall (meaning the X value does not change anymore). He then falls very slowly to the ground.
The character starts at X:0 Y:0 if that's of any help.
How do I make the character to slow down his gain in height the higher he is?
As no one seems to have an answer I tried ins$$anonymous$$d of using a higher drag, to give the rigidbody a higher mass. Weirdly, after adding force to the rigidbody, the rb does not care about its mass anymore. Can someone explain?
Answer by CiberX15 · Mar 25, 2021 at 03:58 AM
Sounds like your problem is mostly coming from applying drag in all directions. Below are a few ways to apply a drag-like effect exclusively on the Y-axis of the velocity.
//This method removes a percentage of the velocity every frame and gets closer to zeroing that velocity the closer to max_height it gets.
float max_height = 100;
private void Update()
{
height = transform.position.y;
float reduceSpeadPercentage = height / max_height;
Vector3 velocity = rb.velocity;
velocity.y -= velocity.y * reduceSpeadPercentage;
rb.velocity = velocity;
}
//This method applies an increasingly powerful downward force the higher you go. Which will make objects flung up higher, fall down faster.
public float height_slowdown = 0.1f;
void Update()
{
height = transform.position.y;
float reduceSpeadPercentage = height / max_height;
Vector3 velocity = rb.velocity;
velocity.y -= height * height_slowdown;
rb.velocity = velocity;
}
Your answer
Follow this Question
Related Questions
Physics are different in Build and Editor 0 Answers
Adding force to rigidbody2d to slide 1 Answer
How to set rigidbody force 1 Answer
Acceleration and Max Speed on 2D Object 0 Answers
Add a force at an angle 1 Answer