Ejemplo n.º 1
0
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (currentCover != null)
        {
            Debug.Log("Enemy is taking cover");

            if (AIFunction.LineOfSight(currentCover.position, attacker, coverCriteria))
            {
                Debug.Log("Cover is compromised");
                // Reset and find a new cover point
                currentCover = null;
                //currentCover = FindCover(attacker, ai.na, coverCheckRadius, numberOfChecks, coverCriteria);
            }
        }

        if (currentCover == null)
        {
            currentCover = FindCover(attacker, ai.na, coverCheckRadius, numberOfChecks, coverCriteria);
        }

        if (currentCover != null)
        {
            Debug.Log("Setting destination for " + ai.name + " from TakeCover behaviour");
            ai.na.SetDestination(currentCover.position);
        }
    }
Ejemplo n.º 2
0
    public NullableVector3 FindCover(Transform attacker, NavMeshAgent na, float coverCheckRadius, int numberOfChecks, LayerMask coverCriteria)
    {
        NullableVector3 cover     = null;
        NavMeshPath     coverPath = null;

        for (int i = 0; i < numberOfChecks; i++)
        {
            // Obtains a random position within a certain vicinity of the agent
            Vector3    randomPosition = ai.transform.position + Random.insideUnitSphere * coverCheckRadius;
            NavMeshHit coverCheck;
            // Checks if there is an actual point on the navmesh close to the randomly selected position
            if (NavMesh.SamplePosition(randomPosition, out coverCheck, na.height * 2, NavMesh.AllAreas))
            {
                if (AIFunction.LineOfSight(coverCheck.position, attacker, coverCriteria) == false) // If line of sight is not established
                {
                    // Ensures that the agent can actually move to the cover position.
                    NavMeshPath newPathToTest = new NavMeshPath();
                    if (na.CalculatePath(coverCheck.position, newPathToTest))
                    {
                        // Checks if the new cover position is easier to get to than the old one.
                        if (cover == null || AIFunction.NavMeshPathLength(newPathToTest) < AIFunction.NavMeshPathLength(coverPath)) // Use OR statement, and check navmesh path cost between transform.position and the cover point currently being checked.
                        {
                            // If so, new cover position is established, and navmesh path is stored for next comparison
                            cover     = new NullableVector3(coverCheck.position);
                            coverPath = newPathToTest;
                        }
                    }
                }
            }
        }

        return(cover);
    }
Ejemplo n.º 3
0
    Character AcquireTarget()
    {
        // Use Physics.OverlapSphere

        Collider[] thingsInEnvironment = Physics.OverlapSphere(LookOrigin, viewRange);
        foreach (Collider thing in thingsInEnvironment)
        {
            /*
             * if (AIFunction.LineOfSightCheckWithExceptions(LookOrigin, thing.transform.position, viewDetection, characterData.HealthData.hitboxes, thing))
             * {
             *
             * }
             */
            if (AIFunction.LineOfSight(LookOrigin, thing.transform, viewDetection))
            {
                //print("Line of sight established between agent and " + thing.name);
                Character targetCharacter = thing.transform.root.GetComponent <Character>();
                if (targetCharacter != null && characterData.HostileTowards(targetCharacter))
                {
                    return(targetCharacter);
                }
            }
        }

        return(null);
    }