示例#1
0
        // Function checking the surroundings for potential targets
        void CheckForPotentialTargets()
        {
            // Create variables
            bool       targetFound             = false;
            GameObject nearestTarget           = null;
            float      distanceToNearestTarget = Mathf.Infinity;

            // Find all potential targets (objects with the "PlayerFaction" tag)
            GameObject[] potentialTargets = GameObject.FindGameObjectsWithTag("PlayerFaction");

            // Analyze each potential target
            foreach (GameObject targetCharacter in potentialTargets)
            {
                // Get the target's Combat Character component
                CombatCharacter targetCombatCharacter = targetCharacter.GetComponent <CombatCharacter>();

                // Check if the target is a viable one
                bool isAViableTarget = true;
                if (targetCombatCharacter == null)
                {
                    // The target is not a combat character
                    isAViableTarget = false;
                }
                else if (targetCombatCharacter.isDead)
                {
                    // The target is dead
                    isAViableTarget = false;
                }

                if (isAViableTarget)
                {
                    // Calculate the distance to the target
                    float distanceToTargetCharacter = Vector3.Distance(targetCharacter.transform.position, transform.position);

                    // Check that the target is under the detection range (otherwise, ignore it completely)
                    if (distanceToTargetCharacter <= enemy.detectionRange)
                    {
                        // Check that the target is visible (otherwise, ignore it completely)
                        if (character.IsVisible(targetCharacter))
                        {
                            // Check that the target is the nearest one
                            if (distanceToTargetCharacter < distanceToNearestTarget)
                            {
                                targetFound             = true;
                                nearestTarget           = targetCharacter;
                                distanceToNearestTarget = distanceToTargetCharacter;
                            }
                        }
                    }
                }
            }

            // If a visible target is in the detection range, alert the character
            if (targetFound)
            {
                Alert(nearestTarget);
            }
        }
示例#2
0
        void Awake()
        {
            // Identify components
            character       = GetComponent <Character>();
            combatCharacter = GetComponent <CombatCharacter>();

            // Identify the Elements
            circleSelector = transform.Find("Circle Selector").gameObject.GetComponent <Projector>();
        }
示例#3
0
 void Awake()
 {
     character       = GetComponent <Character>();
     combatCharacter = GetComponent <CombatCharacter>();
 }
示例#4
0
        IEnumerator CombatBehaviour()
        {
            for (; ;)
            {
                // Determine if the combat behaviour should be ran
                bool shouldRunCombatBehaviour = true;

                if (isDead) // The character is dead
                {
                    shouldRunCombatBehaviour = false;
                }
                else if (!target) // The character has no target
                {
                    shouldRunCombatBehaviour = false;
                    distanceToTarget         = Mathf.Infinity;
                }

                // Running the combat behaviour
                if (shouldRunCombatBehaviour)
                {
                    // Get the target Combat Character component
                    CombatCharacter targetCombatCharacter = target.GetComponent <CombatCharacter>();

                    // Check that the target is not dead (or else, wipe it as a target)
                    if (!targetCombatCharacter.isDead)
                    {
                        bool shouldPursue = false;

                        // Actualize the distance to the target
                        distanceToTarget = Vector3.Distance(target.transform.position, transform.position);

                        // If the character is close enough to its target, see if it should attack it
                        if (distanceToTarget <= effectiveAttackRange)
                        {
                            // If the Character's movement target was defined as the current target, wipe it (the character doesn't need to move towards it anymore)
                            if (character.movementDestination == target.transform)
                            {
                                character.movementDestination = null;
                            }
                            // TODO: delete 2 previous lines?
                            character.movementDestination = null;

                            // If the last attack was made enough time ago, attack the target
                            if (timeSinceLastAttack >= weapon.attackSpeed)
                            {
                                AutoAttackTarget();
                            }
                        }
                        else
                        {
                            shouldPursue = true;
                        }

                        if (shouldPursue)
                        {
                            // Determine the stopping distance (characters stop nearer from their target than necessary, to make sure they don't block other enemies from attacking)
                            float stoppingDistance = effectiveAttackRange - 1f;

                            // Make the character move to its target
                            navMeshAgent.stoppingDistance = stoppingDistance;
                            character.movementDestination = target.transform;
                        }
                    }
                    else
                    {
                        // The target is dead, wipe it
                        target = null;
                    }
                }

                yield return(new WaitForSeconds(0.2f));
            }
        }