How can I make enemies follow a target while being knockback-able?

In my top-down 2D game, the enemies have a simple AI to just move straight towards the player. Originally, I was just doing this with the following code on the enemy:

void FixedUpdate() {
    var newPos = Vector3.MoveTowards(transform.position, PlayerData.Position, MoveSpeed * Time.fixedDeltaTime);
    rbody.MovePosition(newPos);
}

Then, I wanted the enemies to get knocked back when hit by a projectile, which I do with this function on the enemy:
(enemies have a mass of 0.5, linear damping of 0.01, and are dynamic)

public void TakeKnockback(Vector2 amount) {
    rbody.AddForce(amount, ForceMode2D.Impulse);
}

The projectile is kinematic with a trigger collider since I want it to move through enemies. In its OnTriggerEnter2D, I call TakeKnockback like this (other is the Collider2D parameter, knockback is 100f):

enemy.TakeKnockback((other.transform.position - transform.position).normalized * knockback);

As you may have guessed, the enemies don’t get knocked back, because in the first code block, their position is being directly modified, overriding the changes in position due to the knockback force.

So my question is:
How can I make enemies follow a target while being knockback-able?

I’ve seen lots of explanations of how to make an enemy follow a target, and almost all of them say “just use Rigidbody2D.MovePosition toward the target”, and I’ve seen lots of explanations of how to apply knockback to an enemy using AddForce with an impulse force, but these two are mutually exclusive, unless I’m missing something.
I also saw one thing claiming there was a Velocity force mode, but maybe that only exists with 3D rigidbodies? There is no ForceMode2D.Velocity, only Force and Impulse.
Another thing I tried was to add a Force force based on the difference between the target velocity and the actual velocity, but it caused the enemies to just start sliding around everywhere. Maybe I didn’t have the math right, but even then, I can’t imagine this working in a way where the enemies don’t slide at all.

Do you mean ForceMode.Acceleration or ForceMode.VelocityChange here? You can get the same effect as those in 2D, as pretty much all they do is multiply the force by the rigidbody’s mass.

In any case, when you use a rigidbody movement system, it helps to pick one form of moving the ridigbody and sticking with that across the project. So if you’re moving with Rigidbody.MovePosition, then any ‘forces’ need to use that too.

Whatever post I was reading might have been outdated because it just said ForceMode.Velocity, but regardless, reading both of them, neither of them would be a good substitute for regular movement towards something.

And surely you aren’t telling me theres this entire physics system in Unity but I’m going to have to ignore it to write my own version of physics on top of MovePosition just so I can knockback enemies?

The solution I’m currently trying to go with is disabling the “AI” of the enemy while its being knocked back, but I’m having a hard time finding a formula to calculate how long to disable it in a way that scales nicely with knockback force, mass, and linear damping parameters.

They can work fine. It’s just a matter of calculating the right direction with the right magnitude.

Not saying that at all. I’m just saying you need to move a rigidbody in a consistent manner to get the best results. Ergo, only AddForce or only MovePosition or only directly setting the Rigidbody.velocity property. That’s the best way to ensure things work smoothly and as expected. In some cases you can have overlay, such as sometimes directly setting a rigidbody velocity when using AddForce.

I’ve always stuck to just using AddForce and similar with some direct velocity control. And for snappy movement it’s just a case of force * Rigidbody2D.mass to get the same effect as the 3D ForceMode.Acceleration.

Yes, by using MovePosition you miss out on things like inertia and deflection or knock back. It’s why you’ll rarely see a 2D platform game use MovePosition, because the developers want their character to be able to jump and so it’s much easier to let the physics engine take care of that.

Like Spiney says, you could use AddForce to move things around and you can prevent sliding by increasing the linear drag setting on the rigidbody or add some damping to the force calculation.