Knockback direction always the same

Whenever my player is attacked by an enemy I want him to be knocked in the direction away from the enemy. Instead he always get’s knocked the way he isn’t facing.

This is the part of the script which controls the knockback:

    if (other.gameObject.name == "enemySword" && !damaged){
    var target : Transform = other.transform;
    health -= 10;
    dir1 = transform.position - target.position.normalized;
    rb.AddRelativeForce(dir1 * thrust);
    damaged = true;
    damageWait();
    }
}

2 Answers

2

you should change code to this :

//dir1 = transform.position - target.position.normalized;
dir1 = (transform.position - target.position).normalized;

I tried that earlier and the character no longer gets knocked back at all.

Or change line 5 to …

rb.AddRelativeForce(-Vector3.forward * thrust);

This makes the character not get knocked back at all. Although technically it should produce the same problem I'm having. With the character getting knocked backwards even if he gets hit from a different direction.