void OnTriggerEnter2D(Collider2D collision) { /* Player hits Enemy */ Enemy enemy = collision.gameObject.GetComponent <Enemy>(); // Dirty hack to prevent sword colliding with other colliders in the same game object if (m_isAttacking && enemy != null && collision.gameObject.tag.Equals("Enemy") && (collision as CapsuleCollider2D) != null) { enemy.TakeDamage(damage); float knockbackDir = this.transform.parent.transform.localScale.x; enemy.GetComponent <Rigidbody2D>().AddForce(new Vector2(knockbackDir * knockbackForce, 400)); } /* Player hits a Button */ Button button = collision.gameObject.GetComponent <Button>(); if (m_isAttacking && button != null) { button.Press(); } /* Enemy hits player */ CapsuleController player = collision.gameObject.GetComponent <CapsuleController>(); if (m_isAttacking && player != null && collision.gameObject.tag.Equals("Player") && (collision as CapsuleCollider2D) != null) { player.TakeDamage(damage); float knockbackDir = this.transform.parent.transform.localScale.x; player.GetComponent <Rigidbody2D>().AddForce(new Vector2(knockbackDir * knockbackForce, 400)); } }
void OnTriggerEnter2D(Collider2D collision) { CapsuleController player = collision.gameObject.GetComponent <CapsuleController>(); if (player != null) { player.TakeDamage(damage); Destroy(gameObject); float knockbackDir = this.transform.localScale.normalized.x * -1; if (force > 0) { player.GetComponent <Rigidbody2D>().AddForce(new Vector2(knockbackDir * force, 400)); } print(force); } else if (collision.gameObject.tag == "Wall") { Destroy(gameObject); } }