I am trying to make and enemy character bounce back after taking damage.

My game is a top-down 2D and I don’t know how to detect what direction a bullet came from and how to push the enemy back in that direction. For my gun, I did the prefab method of shooting. If anyone can help me that would be great. Thanks so much!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
public int Health = 100;
public ParticleSystem PS;
public Transform enemypos;
public Rigidbody2D rb;
    public void Hit(int damage){
    Health -= damage;
    rb.AddForce(0,0)
    if(Health <= 0){
    die();
    }
    }
    void die(){
    Instantiate(PS, enemypos.position, enemypos.rotation);
    Destroy(gameObject);
    }
}

You can make a bool to detect the “knock from right” that is activated by a comparison of the transform.position.x of your bullet and the object that’s hitted

So I can think two ways that might be possible,. if your bullets are using rigidbody you can always grab the rigidbody.velocity and send the enemy in that direction, or when you instantiate the bullet save the bullets direction it is going as a vector then once you hit an enemy you can use it to figure out which direction to send the enemy