0
\$\begingroup\$

I've got the first part of the code working. It iterates and does a check through the gameobjects to find the furthest nav point to hit. Then it calculates path to see if its valid or not. Now I'm stuck. I want it to re iterate by omitting that value from the list of gameobjects and pick a new value and check it again to see if it is valid. Here is the code in question.

if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
                {

                    furthestObject = FindFurthestObject(navPoints);
                    bool isPathValid =CheckForValidPath(furthestObject.transform.position);

                    if (isPathValid)
                    {
                        navMeshAgent.Warp(furthestObject.transform.position);
                        runFromLight = false;
                    }
                    else
                    {
                        GameObject navPoint = FindRandomSpawnPoint(navPoints);
                        runFromLight = false;
                        GameObject telePortParts = Instantiate(teleportParticleSystem, transform.position, transform.rotation);                            
                        navMeshAgent.Warp(navpoints[0].transform.position);
                    }
                }

And the code for iterating through the gameobject lists is here.

public GameObject FindFurthestObject(List <GameObject> gameObjects)
{
    float furthestDistance = 0;
    GameObject furthestObject = null;

    foreach(GameObject Object in gameObjects)
    {
        float objectDistance = Vector3.Distance(transform.position, Object.transform.position);

        if(objectDistance > furthestDistance)
        {
            furthestObject = Object;
            furthestDistance = objectDistance;
        }
    }

    return furthestObject;
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

create a list of game objects called something like gameObjectsCheck. and make it a copy of your navPoints list, replace

furthestObject = FindFurthestObject(navPoints);

with

furthestObject = FindFurthestObject(gameObjectsCheck);

then, in your first code block, you should surround the entire thing after the initial if statement in a while loop using !isValidPath as it's break condition. this way you will keeping checking new objects until a valid path is found. you will need to create the isValidPath bool outside of the while loop, and set it to false as well, in order to start the while loop.

if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
{
    bool isPathValid = false;
    while ( !isValidPath ) 
    {
        furthestObject = FindFurthestObject(gameObjectsCheck);       
        isPathValid=CheckForValidPath(furthestObject.transform.position);

...

lastly, when you check if a path is valid, in the else statement, as in when a valid path is not found, remove furthestObject from gameObjectsCheck.

in total, this will loop through the code checking whether the current furthest object has a valid path to it or not, and if it doesn't, it is removed from the list of objects you are checking using FindFurthestObject, which will then find the next furthest object, and check again with that object, repeating this process until a valid path is found.

\$\endgroup\$
1
  • \$\begingroup\$ Okay will definitely give it a try. Thanks. Just trying to wrap my head around it. \$\endgroup\$ Jan 22 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .