Beispiel #1
0
    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();
        }
    }
Beispiel #2
0
    //--------------------------------------------------------------------------------
    // 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();
        }
    }