private void OnCollisionEnter2D(Collision2D hit) { if (gameObject.tag == "PlayerWeapon" && hit.gameObject.CompareTag("Player")) { return; } GetComponent <Collider2D>().isTrigger = true; transform.parent = hit.gameObject.transform; rb.velocity = Vector2.zero; if (hit.gameObject.CompareTag("Player") || hit.gameObject.CompareTag("Enemy")) { Rigidbody2D rb = hit.transform.GetComponent <Rigidbody2D>(); hit.transform.GetComponent <HealthManager>().TakeDamage(attackDamage); if (hit.gameObject.CompareTag("Enemy")) { EnemyBaseClass ebc = rb.GetComponent <EnemyBaseClass>(); ebc.ChangeState(EnemyState.stagger); ebc.ChangeState(EnemyState.idle, knockbackTime); } Vector2 direction = hit.transform.position - transform.position; rb.AddForce(direction.normalized * knockbackStrength, ForceMode2D.Impulse); } }
public void PlayerMeeleAttackCalled(LayerMask hitLayers, Transform attackPoint, int attackDamage, float attackRange, float knockbackStrength, float knockbackTime) { Collider2D[] hitRegistered = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, hitLayers); foreach (Collider2D hit in hitRegistered) { //print("hit " + enemy.name); hit.GetComponent <HealthManager>().TakeDamage(attackDamage); Rigidbody2D rb = hit.GetComponent <Rigidbody2D>(); if (rb != null) { EnemyBaseClass ebc = rb.GetComponent <EnemyBaseClass>(); ebc.ChangeState(EnemyState.stagger); Vector2 direction = hit.transform.position - transform.position; rb.AddForce(direction.normalized * knockbackStrength, ForceMode2D.Impulse); ebc.ChangeState(EnemyState.idle, knockbackTime); } } }