示例#1
0
    // Update is called once per frame
    public void UpdateState()
    {
        // Return to patrol state if target is out of range
        if (!stateMachine.IsTargetInRange())
        {
            ToPatrolState();
        }

        // Get the direction from self to target
        Vector3 targetDir = stateMachine.GetTargetDirection();

        // Calculate movement step speed
        float step = stateMachine.maxChaseSpeed * Time.deltaTime;

        // Calculate new look rotation
        Vector3 newDir = Vector3.RotateTowards(stateMachine.rotationBody.forward, targetDir, step, 0.0f);

        stateMachine.rotationBody.rotation = Quaternion.LookRotation(newDir);

        // Reset X and Z axis
        Vector3 tempEulerAngles = stateMachine.rotationBody.eulerAngles;

        tempEulerAngles.x = 0.0f;
        tempEulerAngles.z = 0.0f;
        stateMachine.rotationBody.eulerAngles = tempEulerAngles;

        // Attack after timer
        if (chaseTime >= stateMachine.chaseDuration)
        {
            ToAttackState();
        }

        // Increment timer
        chaseTime += Time.deltaTime;
    }
    void CheckTargetIsInFOV()
    {
        if (!stateMachine.IsTargetInRange())
        {
            return;
        }

        Vector3 targetDir     = stateMachine.GetTargetDirection();
        float   angleToTarget = Vector3.Angle(targetDir, stateMachine.rotationBody.forward);

        // Check if target is in FOV
        if (angleToTarget <= stateMachine.turrentLookFOV * 0.5f && angleToTarget >= -stateMachine.turrentLookFOV * 0.5f)
        {
            ToAlertState();
        }
    }