Пример #1
0
    /// <summary>
    /// Calculate the angle we need to turn the AI Entity body towards the target
    /// </summary>
    /// <returns>A positive angle in degrees that is needed to face the target.</returns>
    public float DetermineAngleNeededToTurnTowardsTarget()
    {
        float angle = CalculationUtil.FindSignedAngle(
            this.stateMachine.AiEntityBodyTransform.forward,
            this.stateMachine.NavAgent.steeringTarget - this.stateMachine.AiEntityBodyTransform.position
            );

        return(angle);
    }
Пример #2
0
    /// <summary>
    /// Handles the alerted state by continuing to pursue waypoints, etc.
    /// </summary>
    /// <returns>Either the alerted state or any new state if certain conditions were met.</returns>
    private AiStateType HandleDefaultState()
    {
        AiStateType state = GetDefaultStateType();

        if (
            !zombieStateMachine.IsTargetReached &&
            (zombieStateMachine.ThreatManager.DoesAudioThreatExist() || zombieStateMachine.ThreatManager.DoesLightThreatExist())
            )
        {
            // we got close to the target due to light or sound, so now we need to know if we should pursue it or seek to find it
            float angle = CalculationUtil.FindSignedAngle(
                zombieStateMachine.AiEntityBodyTransform.forward,
                zombieStateMachine.ThreatManager.CurrentTarget.Position - zombieStateMachine.AiEntityBodyTransform.position
                );

            if (zombieStateMachine.ThreatManager.DoesAudioThreatExist() && Mathf.Abs(angle) < this.threatAngleThreshold)
            {
                // it's a sound and we are capable of heading to it, so pursue it
                state = AiStateType.Pursuit;
            }
            else if (HasReachedMaxDirectionChangeTime())
            {
                // it's not a sound and we are not capable of turning towards it, so determine which way we should turn
                if (Random.value < zombieStateMachine.Intelligence)
                {
                    SeekTowards(angle); // smartly turn
                }
                else
                {
                    SeekRandomly();          // randomly turn because we are a stupid zombie :)
                }
                ResetDirectionChangeTimer(); // TODO: this does sometimes happen often and it makes the zombie appear as if it is stuck in the alerted state
            }
        }
        else if (
            zombieStateMachine.ThreatManager.IsTargeting(AiTargetType.Waypoint) &&
            !zombieStateMachine.NavAgent.pathPending
            )
        {
            // we were targeting a waypoint and we arrived at it, so determine if we can head to next one or turn towards it
            float angle = zombieStateMachine.ThreatManager.DetermineAngleNeededToTurnTowardsTarget();

            if (Mathf.Abs(angle) < this.turnOnSpotThreshold)
            {
                state = AiStateType.Patrol;
            }
            else if (HasReachedMaxDirectionChangeTime())
            {
                SeekTowards(angle);
                ResetDirectionChangeTimer();
            }
        }
        else if (HasReachedMaxDirectionChangeTime())
        {
            // we didn't find what we were looking for and our clock ran out, so turn randomly and repeat the entire alert process
            // TODO: this does sometimes happen often and it makes the zombie appear as if it is stuck in the alerted state
            SeekRandomly();
            ResetDirectionChangeTimer();
        }

        return(state);
    }