示例#1
0
    private void Update()
    {
        facing = Mathf.Sign(transform.localScale.x);

        playerPosition  = playerTransform.position;
        playerDirection = (playerPosition - lantern.transform.position).normalized;

        bool isWalking = false;

        if (doMovement && state != State.Recharging && state != State.Turning)
        {
            if (UpdateMovement())
            {
                isWalking = true;
            }
        }

        animator.SetBool(isWalkingHash, isWalking);

        if (state == State.Tracking && !isPlayerConsideredVisible)
        {
            if (playerLooseTimer.Tick())
            {
                state        = State.Patrolling;
                lanternAngle = 0;
            }
        }

        if (state == State.Following || state == State.Tracking)
        {
            // Facing towards the player.
            SetFacing(Mathf.Sign(playerPosition.x - transform.position.x));
            // Angle lantern towards player.
            lanternAngle = MathX.Angle(playerDirection) * Mathf.Rad2Deg;
            if (facing < 0)
            {
                lanternAngle = 180f - lanternAngle;
            }
        }

        lantern.Rotation = lanternAngle;
        if (state != State.Recharging)
        {
            UpdatePlayerVisibility();
        }

        if (looseVisibilityTimer.Tick())
        {
            state = State.Tracking;
            isPlayerConsideredVisible = false;
            playerLooseTimer.Reset(playerForgetTime);
            OnLost();
        }

        animator.SetBool(isAttackingHash, isPlayerConsideredVisible);

        if (state == State.Following)
        {
            Debug.Assert(isPlayerConsideredVisible, "isPlayerVisible && state == State.Following");

            if (damageTimer.Tick())
            {
                StartCoroutine(CoRechargeLantern());
                playerHealth.TakeDamage(damage);

                onDamage.Invoke();
            }
        }
    }