How to calculate a force to nullify/compensate for drag?

I’m trying to wrap my head around Drag works in Unity physics, and I’m getting nowhere.

Simply put, I’m making a space shoot 'em up and I want to use drag for simplified breaking thrusters to slow the ship down to a stop, when hands are off the controls. At the same time I want to be able to apply relative force to the ship in any given direction, with constant acceleration regardless of how high or low the drag is set.

I know there is a forcemode for acceleration, but I still need “mass” to be part of the equation, so I just want to be able to counter the drag without having to change the thruster force parameters in the inspect.

How could I do this?

This is what my maneuvering function looks like, that runs in FixedUpdate.

    private void FireThrusters()
    {
        var targetSpeed = new Vector2(MaxSpeed * direction.x, MaxSpeed * direction.y);

        if (direction.y > 0.0f && rb.velocity.z < targetSpeed.y)
            rb.AddRelativeForce(Vector3.forward * Thrust);
        else if (direction.y < 0.0 && rb.velocity.z > targetSpeed.y)
            rb.AddRelativeForce(-Vector3.forward * Thrust);

        if (direction.x > 0.0f && rb.velocity.x < targetSpeed.x)
            rb.AddRelativeForce(Vector3.right * Thrust);
        else if (direction.x < 0.0 && rb.velocity.x > targetSpeed.x)
            rb.AddRelativeForce(-Vector3.right * Thrust);

        CameraCorrectMovement();
    }

I did experiment with creating actual breaking thrusters, and setting drag ot 0, but that resulted in an odd anomaly, where the ship would drift efter so slightly after any kind of collision, but still register as having a velocity of 0.

Hi,

If you want to counter drag, you can just set the Rigidbody.drag to 0 when you don’t want it, and restore it as the previous value when you want drag to be applied again.

Another way to do so is to apply manual drag whenever you need it : you can just add a force at the opposite of your velocity vector :
rb.AddRelativeForce(-rb.velocity * dragFactor);

I hope this helps you get through this !

The first suggestion unfortunately just doesn’t work since I’m working with vectored thrust, and I still need to be able to come to a stop along one axis, while still applying thrust along another.

The second is more or less what I have been doing, but with axis aligned breaking thrust,.But having a drag of 0 is causing odd behavior with the rigidbody picking up momentum from collisions that cannot be countered by adding forces.

Example: I collide with an enemy ship and pick up a slight drift to the right side of the screen. I apply thrust to the left, but as soon as I release the key and apply enough force come to a stop, it begins drifting back to the right. =/