private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Enemy")) { // When colliding with an enemy we want the player to knock them back unless they're moving slower. // first check who's going faster AIAgent AI = collision.gameObject.GetComponent <AIAgent>(); if (velocity.magnitude > AI.Velocity.magnitude) { // player going faster means we knock the AI back instead of us AI.AddStun(); // calculate force vector var force = collision.transform.position - transform.position; // normalize force vector to get direction only and trim magnitude force.Normalize(); AI.GetComponent <Rigidbody>().AddForce(force * shipKnockBack); } else { // Stun stops the player controller from moving forward and allows us to add forces to the player controller.AddStun(stunDuration); // calculate force vector var force = transform.position - collision.transform.position; // normalize force vector to get direction only and trim magnitude force.Normalize(); gameObject.GetComponent <Rigidbody>().AddForce(force * shipKnockBack); } } if (collision.contacts[0].thisCollider.CompareTag("Ram")) { float hitDamage = collision.relativeVelocity.magnitude; // Debug.Log("Hit with Ram with a force of " + hitDamage); if (collision.collider.gameObject.GetComponent <AttachmentBase>()) { collision.collider.gameObject.GetComponent <AttachmentBase>().TakeDamage(ramDamage); } if (collision.collider.gameObject.GetComponent <AIAgent>()) { collision.collider.gameObject.GetComponent <AIAgent>().TakeDamage(ramDamage); } } if (collision.gameObject.layer == 8) { // Stun stops the player controller from moving forward and allows us to add forces to the player controller.AddStun(islandStunDuration); // calculate force vector var force = transform.position - collision.transform.position; // normalize force vector to get direction only and trim magnitude force.Normalize(); gameObject.GetComponent <Rigidbody>().AddForce(force * islandKnockBack); } }