/// <summary> /// Knocks player back upon contact and deducts HP. /// </summary> /// <param name="dir">Direction of the knockback.</param> public void Knockback(Vector3 dir) { playerRB.AddForce(dir * 10, ForceMode.Impulse); playerRB.velocity = Vector3.zero; playerCM.Damage(damage); }
private void OnCollisionEnter(Collision collision) { if (alive && damageOnCollision) { if (collision.gameObject.tag == "Player") { CombatManager enemyCM = collision.gameObject.transform.GetComponent <CombatManager>(); // If enemy is a combatable object, calculate and deal damage to it if (enemyCM) { int damage = enemyStats.GetDamagePower(); enemyCM.Damage(damage); Rigidbody playerRB = collision.gameObject.GetComponent <Rigidbody>(); Vector3 targetDir = collision.transform.position - transform.position; playerRB.AddForce(targetDir.normalized * 10, ForceMode.Impulse); playerRB.velocity = Vector3.zero; } } } }
private void OnTriggerEnter(Collider other) { // Player's weapon if (controller) { if (other.gameObject.tag == "Enemy") { CombatManager enemyCM = other.transform.gameObject.GetComponent <CombatManager>(); // If enemy is a combatable object, calculate and deal damage to it if (enemyCM) { int damage = stats.GetDamagePower(); //print("player dealt: " + damage + " damage."); enemyCM.Damage(damage); } } } // Enemy's weapon if (enemyController) { if (other.gameObject.tag == "Player") { CombatManager playerCM = other.transform.gameObject.GetComponent <CombatManager>(); // If enemy is a combatable object, calculate and deal damage to it if (playerCM) { int damage = stats.GetDamagePower(); //print("enemy dealt: " + damage + " damage."); playerCM.Damage(damage); } } } }