Why doesn't this code apply force to the gameobject in the opposite direction?


The debug returns when I run into an enemy so I know that the if statement is being reached. However, the enemy just continues running into the player only being held back by its collider. The object this script is attached to has a boxcollider2d set as a trigger, as well as a child object that holds a non-trigger boxcollider2d.
None of the rigidbody2D’s are set to kinematic either (all set to dynamic). The player rigidbody has a mass of 10, and a gravity scale of 0; the enemy rigidbody has a mass of 1 and a gravity scale of 0

You don’t need Time.DeltaTime on line 25. There is a lot of confusion about when to use Time.DeltaTime but think of it this way: it’s mainly for distance (linear or angular). If I want to add a force, it doesn’t matter how long it’s been since the last frame update. I just want to add the total force now. Same with setting rigid body velocity. I could say “go at 50 km/h” every mill-second or every second. The length of time doesn’t matter. Velocity and force are independent or frame rate.

Multiplying by Time.DeltaTime makes the force very small so you’re not seeing it.