예제 #1
0
    //Could use some work
    //Improvements: Add smoothing instead of agent.isStopped = true / false.
    private void Attack()
    {
        if (!chasing || endState)
        {
            return;
        }

        attacking = true;
        Rigidbody rb = GetComponent <Rigidbody>();

        agent.isStopped = true;

        Ray        ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        Physics.Raycast(ray, out hit, 4);
        if (hit.collider == null)
        {
            Quaternion targetRot = Quaternion.LookRotation((player.transform.position - transform.position).normalized);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, 3.0f);
            rb.MoveRotation(targetRot);
        }
        else if (hit.collider.tag != "Player")
        {
            Quaternion targetRot = Quaternion.LookRotation((player.transform.position - transform.position).normalized);
            rb.MoveRotation(targetRot);
        }

        anim.SetTrigger("Attack");

        if (playerHealth.health > 0)
        {
            StartCoroutine(AttackAnimDelay());
            playDamage = StartCoroutine(playerAudio.PlayDamage());
        }
    }