knockback on collision with enemy

in my 2d top down game, i want my player to experience knockback when they get in contact with an enemy but i cant seem to simulate it no matter what i do! heres the problem. i get hit the right amount then the next time i get hit i get hit twice the amount and it keeps on knocking me back more so i just deleted it
can anyone show me how to add knockback? (im kinda stupid :P)

heres movement code:

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

public class Movement : MonoBehaviour
{
    [Header("Movement and Rotation")]
    public int moveSpeed;
    Vector2 movement;
    Vector2 mousePos;

    [Header("Knockback")]
    public float kb;
    public float kbStunTime;

    bool canMove;

    [Header("References")]
    public Camera cam;
    public Rigidbody2D playerRb;

    private void Start()
    {
        canMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }

    private void FixedUpdate()
    {
        if (canMove)
        {
            playerRb.MovePosition(playerRb.position + (movement * moveSpeed * Time.fixedDeltaTime));
        }

        Vector2 lookDir = mousePos - playerRb.position;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
        playerRb.rotation = angle;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.CompareTag("Enemy"))
        {
            //Vector2 dir = transform.position - collision.gameObject.transform.position;
            canMove = false;
            //playerRb.AddForce(dir * kb, ForceMode2D.Impulse);
            StartCoroutine(KnockbackStunTime(kbStunTime));
        }
    }

    IEnumerator KnockbackStunTime(float cooldown)
    {
        yield return new WaitForSeconds(cooldown);
        canMove = true;
    }
}

It would help if you put in your original knockback code so we know how you approached it. Based on on that it should be simple to change it the way it should work.

It should simply be a case of reversing the exact same code for your knockback force but having the code affect the player rather than the enemy when you collide with the enemy.

i dont rly have knockback for when i hit my enemy right now. im talking about knockback to the PLAYER when you hit the ENEMY. ;-;

i tried making a vector2 that calculates the direction and then using rigidbody.addforce but its rly unreliable

It’s also a bit unnecessary for you to use a coroutine for something as simple as a timer, set the float to the original time you want and use -= Time.DeltaTime to count down instead.

ok thanks i will check out the video

How would this be done in Unreal Blueprint??

You should ask this in the Unreal Engine forum or some other forum where people talk about Unreal Engine.