Need help fixing my 2D player knockback script

Need help. Hello all

I want the knock back to happen to the player on either side of the enemy. for now it only activates when he either jumps on the enemy or walk into the enemy from the left side. Obviously i want it to activate the player knock back from the right, top and left side of the enemy. Keep in mind its a 2D platformer style like castlevania so im trying to recreate that same knock back damage effect from those old games

This is as far as i got with the code that makes the knock back code touching the left side of the enemy to activate the effect:

    float hit = 4500f;
    public float knockbackForce = 10f;
    public float knockbackDuration = 0.5f;

    public bool isKnockback = false;
    private Vector2 knockbackDirection;


private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            TakeDamage(10);
            StartCoroutine(KnockbackCoroutine());
          
        }
    }


  IEnumerator KnockbackCoroutine() 
    {
        isKnockback = true; 
        Vector2 difference = (transform.position - Enemy.transform.position).normalized;
        Vector2 force = difference * knockbackForce;
        rb.AddForce(-transform.right * hit);
        rb.AddForce(force, ForceMode2D.Impulse);

        yield return new WaitForSeconds(knockbackDuration);

        isKnockback = false; 
    }

What am i missing for the full effect touching any side of the enemy to get knock back?

You just need a direction from the player to the enemy. How to do this is outlined in the documentation: Unity - Manual: Vectors

So when you collider with an enemy, just get said direction, and the reverse of that direction is the direction to not the player back towards.

Ill be honest with you. I learn slow at this stuff right now and i use either examples to learn or watch step by step tutorials to get as far as i did. What i do know is rb.AddForce(-transform.right * hit); is what got me the partial results

Im seeing it saying Vector3 and im using Vector2. I thought Vector3 was only for 3D stuff? So im lost by reading all that math. If you could show me what you would do to alter what i did a bit to give me the result im looking for id appreciate it. That doc is just showing a lot of math and making me over think my entire code. if its a small fix id like to visually see the example. thats all.

All of Unity is technically 3d. Even 2d objects exist in 3d space in Unity, and all have a transform component with a Vector3 (3d) position. Vector2 is just when you only need to do calculations that operate on a 2d plane, but pretty much 100% of the principles with Vector3 apply to Vector2, the only difference being the latter lacks a z component.

The important line is this:

var c = b - a;

Or in more english terms:

Vector3 direction = targetPosition - currentPosition;

So when your enemy enacts your trigger, you get can this direction, and just pass it to your co-routine method. I think the issue here is your calculation for difference is backwards.

It should be as simple as:

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Enemy"))
    {
        Vector3 direction = -(collision.transform.position - this.transform.position).normalized;
      
        TakeDamage(10);
        StartCoroutine(KnockbackCoroutine(direction));   
    }
}


IEnumerator KnockbackCoroutine(Vector3 direction)
{
    isKnockback = true;
    Vector3 force = direction * knockbackForce;
    rb.AddForce(force, ForceMode2D.Impulse);

    yield return new WaitForSeconds(knockbackDuration);

    isKnockback = false;
}

With the changes it took way the partial knockback effect entirely i had before. But thats fine though. what you explained made me jarred my memory on something else which triggers get hit from both sides from an old Unity Megaman game script.

Thanks anyway!

I’m not sure what ‘partial’ knockback means, but it should work in pushing the player away from the enemy.

when you removed this… rb.AddForce(-transform.right * hit);

the mild effect i had before was gone as well.

i was using float hit = 4500f;

&

rb.AddForce(-transform.right * hit);

I did a test with what your post suggested and nothing was happening at all anymore. I guess its something else i dont see

That will only ever apply a huge force for one physics frame in the one direction upon contact. If you’re trying to mitigate the force of the knockback… just apply less force? Or at the very least, apply it in the opposite direction of your knockback, rather than a static direction.