コード例 #1
0
    private void Update()
    {
        // If Crystal was destroyed, have all enemies stop all movement.
        if (GameController.GameOver())
        {
            if (navMeshAgent.velocity.magnitude > 0.0f)
            {
                DelayReaction();
            }
            return;
        }

        // If no target detected, move towards the Crystal.
        if (attackTarget == null)
        {
            if (movementTimer > movementDelay)
            {
                navMeshAgent.SetDestination(CrystalController.GetInstance().transform.position);
                movementTimer = 0.0f;
            }
        }

        // Target is detected.
        else
        {
            // If close enough to attack target.
            if (Vector3.Distance(attackTarget.transform.position, transform.position) < attackRange)
            {
                navMeshAgent.velocity = Vector3.zero;
                navMeshAgent.ResetPath();

                // Start attacking after delay has been reached.
                if (attackTimer > attackDelay)
                {
                    // Align sprite to always face target.
                    spriteDirection = Vector3.ProjectOnPlane(attackTarget.transform.position - transform.position, Camera.main.transform.forward).normalized;
                    if (spriteAnimator != null)
                    {
                        spriteAnimator.Attack();
                    }

                    // Player sound and remove health from target.
                    AudioController.Attack();
                    StartCoroutine(DelayedAttack(0.2f));
                    attackTimer = 0.0f;
                }

                movementTimer = 0.0f;
                attackTimer  += Time.deltaTime;
            }

            // Not close enough to attack, keep chasing target.
            else if (movementTimer > movementDelay)
            {
                navMeshAgent.SetDestination(attackTarget.transform.position);
                movementTimer = 0.0f;
            }
        }

        movementTimer += Time.deltaTime;
    }