Ejemplo n.º 1
0
 private void OnTriggerEnter(Collider other)
 {
     // If the enemy is attacking then calculate if the player is touching the enemy hand
     if (enemyAttack.GetIsAttacking())
     {
         // If other is the player, then hurt the player
         if (other.CompareTag(Tags.player))
         {
             playerHealth.TakeDamage(meleeDamage);
         }
     }
 }
Ejemplo n.º 2
0
    private void FixedUpdate()
    {
        // If the player is detected then the enemy starts to follow the player
        if (playerDetected && !agent.isStopped)
        {
            bool isAttacking = enemyAttack.GetIsAttacking();

            // If the player is outside the stoppingDistance then make the enemy follow the player
            if (distance > attackRadius)
            {
                if (!isAttacking)
                {
                    // Make the eneny follow the player
                    destination = player.position;
                    agent.SetDestination(destination);

                    character.Move(agent.desiredVelocity, false);
                }
            }
            else
            {
                bool tryingToAttack = enemyAttack.GetTryingToAttack();
                if (!isAttacking && !tryingToAttack)
                {
                    agent.SetDestination(transform.position);

                    // When the enemy is inside attackRadius the enemy can attack
                    enemyAttack.Attack(player, transform, attackRadius);
                }



                if (tryingToAttack)
                {
                    // Make the eneny follow the player
                    destination = player.position;
                    agent.SetDestination(destination);
                    agent.stoppingDistance = 1.5f;
                    character.Move(agent.desiredVelocity, false);
                }
                else
                {
                    character.Move(Vector3.zero, false);
                }
            }

            // Check if the player is at the enemy's back
            float distEnemyPlayer = transform.position.x - player.position.x;

            // If the player is at its back then make it turn around
            Quaternion newRotation = new Quaternion();
            if (distEnemyPlayer >= 0)
            {
                newRotation = Quaternion.Lerp(rb.rotation, Quaternion.Euler(0f, -90f, 0f), turnSmoothing * Time.deltaTime);
            }
            else if (distEnemyPlayer < 0)
            {
                newRotation = Quaternion.Lerp(rb.rotation, Quaternion.Euler(0f, 90f, 0f), turnSmoothing * Time.deltaTime);
            }

            rb.MoveRotation(newRotation);
        }
    }