Unity 2d topdown, my projectile is continuously knocking back my enemy

I’m making a zombie game where the player will shoot a bullet at a zombie and the zombie will die after a certain amount of hits. This is the code that I have for my zombies:

``````    public Transform player;
public float moveSpeed = 5f;
[SerializeField] public float enemyHealth = 100f;
private ZombieSpawner spawner; // Reference to the ZombieSpawner
void Update()
{
if (enemyHealth <= 0)
{
Defeated();
Destroy(gameObject);
return;
}

if (player != null)
{
FollowPlayer();
}
else
{
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
// Subtract one from enemyHealth
enemyHealth--;

// Destroy the bullet on impact
Destroy(collision.gameObject);

// Optional: You may want to add a delay to prevent immediate collisions
// StartCoroutine(DisableCollisionForDuration(0.5f));
}
if (collision.gameObject.CompareTag("FireBullet"))
{
enemyHealth -= 2f;
Destroy(collision.gameObject);
}
if (collision.gameObject.CompareTag("LaserBullet"))
{
enemyHealth -= 5;
Destroy(collision.gameObject);
}
}
public void FollowPlayer()
{
// Determine the direction to the player
Vector2 direction = player.position - transform.position;
direction.Normalize(); // Ensure the direction vector has a length of 1

// Calculate the rotation angle
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90f;

// Rotate the enemy towards the player
transform.rotation = Quaternion.Euler(0f, 0f, angle);
// Move towards the player
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
}
private void Defeated()
{
// Call the NormalEnemyDefeated method on the ZombieSpawner
if (spawner != null)
{
spawner.NormalEnemyDefeated();
// adds 1 to the value of enemies defeated
}

}
``````

When I use this code, however, my zombies kind of jitter around and get continuously knocked back to the point where they can’t move. Can someone help me with this? I don’t understand what I am doing wrong.

The momentum from the bullets are probably getting transferred to the zombie when the collision happens.

One option is to change the bullet’s collider to a trigger. There is an “Is Trigger” checkbox on the colliders in the inspector. In the zombie class you will use `OnTriggerEnter2D` instead of `OnCollisionEnter2D`.

If you need the bullets to use a non-trigger collider, then as a hack you could decrease the bullet’s Mass or increase the zombie’s Mass in their respective RigidBody2Ds.

That worked. For anyone wondering what I changed:

``````private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
Debug.Log("bullet hit zombie");
// Subtract one from enemyHealth
enemyHealth--;

// Destroy the bullet on impact
Destroy(collision.gameObject);

// Optional: You may want to add a delay to prevent immediate collisions
// StartCoroutine(DisableCollisionForDuration(0.5f));
}
if (collision.gameObject.CompareTag("FireBullet"))
{
enemyHealth -= 2f;
Destroy(collision.gameObject);
}
if (collision.gameObject.CompareTag("LaserBullet"))
{
enemyHealth -= 5;
Destroy(collision.gameObject);
}
}
``````