void OnTriggerEnter(Collider other) { bool attacking = animator.GetCurrentAnimatorStateInfo(0).IsName("Special") && !animator.IsInTransition(0); if (other.tag != "Enemy") { return; } if (!attacking) { return; } // Stores current enemy as a GameObject GameObject enemy = other.gameObject; // Gets the Rigidbody component of the current enemy Rigidbody rb = enemy.GetComponent <Rigidbody>(); // Gets the NavMeshAgent component of the current enemy NavMeshAgent agent = enemy.GetComponent <NavMeshAgent>(); // Gets the AI script of the current enemy BasicAIScript AI = enemy.GetComponent <BasicAIScript>(); // Disables the NavMeshAgent of the current enemy agent.enabled = false; // Disables kinematic of the enemy's Rigidbody rb.isKinematic = false; // Vector3 determines the direction the enemy will fly and normalises the variable Vector3 direction = enemy.transform.position - transform.position; direction.Normalize(); // Adds force to make the enemy fly back rb.AddForce(direction * hitForce + Vector3.up * upForce, ForceMode.Impulse); // Decreases the enemies health by how much damage was dealt AI.enemyHealth -= damage; // Checks if enemies health is equal to or goes below zero if (AI.enemyHealth <= 0) { // If so, set the dead bool in AI script to be true for enemy AI.dead = true; } else { agent.enabled = true; rb.isKinematic = true; AI.ResetFlashCoolDown(); AI.Flash(); } }
//-------------------------------------------------------------------------------- // Function determines events when an object stays in a trigger. // // Param: // other: Is the collider of the object triggering the function. //-------------------------------------------------------------------------------- void OnTriggerStay(Collider other) { // Bool determines if an animation is playing bool attacking = animator.GetCurrentAnimatorStateInfo(0).IsName("Attack") && !animator.IsInTransition(0); // Ignores function if the collider of object doesn't have a tag of "Enemy" if (other.tag != "Enemy") { return; } // Ignores function if player is not attacking if (!attacking) { return; } // Sets the current enemy as a GameObject GameObject enemy = other.gameObject; // Gets the Rigidbody of the enemy Rigidbody rb = enemy.GetComponent <Rigidbody>(); // Gets the NavMeshAgent of the enemy NavMeshAgent agent = enemy.GetComponent <NavMeshAgent>(); // Gets the enemy's BasicAIScript BasicAIScript AI = enemy.GetComponent <BasicAIScript>(); BoneParticle.SetActive(true); // Disables NavMeshAgent for enemy agent.enabled = false; // Disables Kinematic for the enemies Rigidbody rb.isKinematic = false; // Direction Vector3 used for direction enemy will be knocked back Vector3 direction = enemy.transform.position - transform.position; // Direction Vector3 normalised direction.Normalize(); // Adds a knockback force that gets applied to the enemy's Rigidbody rb.AddForce(direction * hitForce + Vector3.up * upForce, ForceMode.Impulse); // Decreases the enemies health by how much damage was dealt AI.enemyHealth -= damage; // Checks if enemies health is equal to or goes below zero if (AI.enemyHealth <= 0) { // If so, set the dead bool in AI script to be true for enemy AI.dead = true; rb.detectCollisions = false; } // Else if the enemies health is above zero else { // Re-enables the NavMeshAgent and its Rigidbody agent.enabled = true; rb.isKinematic = true; // Resets enemy's cool down and allows it to flash AI.ResetFlashCoolDown(); AI.Flash(); } }
//------------------------------------------------------------ // Function applies knockback physics to enemies //------------------------------------------------------------ private void Knockback() { // Gets the layer mask of an enemy and stores it into local int int layerMask = 1 << LayerMask.NameToLayer("Enemy"); // Vector3 used to determine the dash direction Vector3 dashDir = endPos - startPos; // Dash Direction Vector3 normalised dashDir.Normalize(); // Defines the centre Vector3 as a point half the distance between start and end position Vector3 centre = (startPos + endPos) * 0.5f; // Creates a "new" Vector3 which is half the size of hitbox Vector3 halfSize = new Vector3(width * 0.5f, 2, dist * 0.5f); // Stores all enemies near striker during dash into a local array Collider[] hitEnemies = Physics.OverlapBox(centre, halfSize, transform.rotation, layerMask); // Runs a for loop for all enemies in local array for (int i = 0; i < hitEnemies.Length; ++i) { // Stores current enemy as a GameObject GameObject enemy = hitEnemies[i].gameObject; // Gets the Rigidbody component of the current enemy Rigidbody rb = enemy.GetComponent <Rigidbody>(); // Gets the NavMeshAgent component of the current enemy NavMeshAgent agent = enemy.GetComponent <NavMeshAgent>(); // Gets the AI script of the current enemy BasicAIScript AI = enemy.GetComponent <BasicAIScript>(); // Sets the enemy to be dead in AI script AI.dead = true; // Disables the NavMeshAgent of the current enemy agent.enabled = false; // Disables kinematic of the enemy's Rigidbody rb.isKinematic = false; // Enables gravity of the enemy's Rigidbody rb.useGravity = true; // Determines how far the enemy will fly back and normalises the Vector3 Vector3 enemyDir = enemy.transform.position - startPos; enemyDir.Normalize(); // Creates a "new" Vector3 which will be used to add force Vector3 rightAngle = new Vector3(dashDir.z, 0, -dashDir.x); // Gets the dot product of rightAngle and enemyDir float dot = Vector3.Dot(rightAngle, enemyDir); // If enemy is to the right of player, add a force to make the enemy fly right if (dot > 0.0f) { rb.AddForce(rightAngle * hitForce + Vector3.up * upForce, ForceMode.Impulse); } // Otherwise, add a force that makes the enemy fly left else { rb.AddForce(-rightAngle * hitForce + Vector3.up * upForce, ForceMode.Impulse); } } }