示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (agent.remainingDistance - meleeAttackDistance < 0.01f)
        {
            if (Time.time > nextAttackTime)
            {
                nextAttackTime = Time.time + attackRate;

                //Attack
                RaycastHit hit;
                if (Physics.Raycast(firePoint.position, firePoint.forward, out hit, meleeAttackDistance))
                {
                    if (hit.transform.CompareTag("Player"))
                    {
                        Debug.DrawLine(firePoint.position, firePoint.position + firePoint.forward * meleeAttackDistance, Color.cyan);

                        IEntity      player       = hit.transform.GetComponent <IEntity>();
                        PlayerHealth playerHealth = hit.transform.GetComponent <PlayerHealth>();
                        playerHealth.TakeDamage(npcMeleeDamage);
                    }
                }
            }

            if (canMoveRandomly && agent.remainingDistance - meleeAttackDistance < 0.01f)
            {
                agent.destination       = agent.destination + new Vector3(Random.Range(-15, 15), Random.Range(-5, 5), Random.Range(-15, 15));
                enemyAnim.currentTarget = agent.destination;
                canMoveRandomly         = false;
                StartCoroutine(WaitForRandomMovementEnd());
            }
        }



        //Move towards the player if see
        RaycastHit hit2;

        if (Physics.Raycast(viewPoint.position, playerTransform.position - viewPoint.position, out hit2, 100))
        {
            if (hit2.collider.gameObject.tag == "Player")
            {
                canSeePlayer      = true;
                agent.destination = playerTransform.position;
                enemyAnim.Shoot(agent.destination);
            }
            else
            {
                canSeePlayer = false;
                StartCoroutine(WaitForRandomMovementEnd());
            }
        }


        //Always look at player if see
        transform.LookAt(new Vector3(agent.destination.x, transform.position.y, agent.destination.z));


        //Gradually reduce rigidbody velocity if the force was applied by the bullet
        r.velocity *= 0.5f;

        if (Random.Range(0, 1000) <= 1)
        {
            audioSource.clip = enemyBreatheSounds[Random.Range(0, enemyBreatheSounds.Length)];
            audioSource.Play();
        }
    }