- Home /
SetDestination's and CalculatePath's Path State Return Works Too Late
Hi all, I'm trying to make a turret defence game. And i'm trying to check if my navmesh agent can reach to target if i place the turret on selected grid by using bool return of SetDestination. The problem is, it reports that path is blocked when i try to place another turret after i placed the turret actually blocking path. Below is my placement function of turret placement script.
Edit: I tried with both SetDestination and CalculatePath. Nothing changed, below is the one i tried with CalculatePath.
Thanks in advance.
void GhostComeAlive ()
{
aGridSelected = false;
//first create a temporary gameobject to check if path can complete itself.
GameObject gridToCheck = lastGridMousePassedOver;
GameObject navMeshCheckerTemp = Instantiate(GhostTurretG, gridToCheck.transform.position, gridToCheck.transform.rotation) as GameObject;
GameObject navMeshChecker = Instantiate(navMeshCheckerTemp, navMeshCheckerTemp.transform.position, navMeshCheckerTemp.transform.rotation) as GameObject;
Destroy (navMeshCheckerTemp);
//calculate the path.
NavMeshPath path = new NavMeshPath();
navMeshValidator.GetComponent<NavMeshAgent>().CalculatePath(navMeshTarget.transform.position, path);
//now check if it's valid.
if (path.status == NavMeshPathStatus.PathComplete)
{
Debug.Log ("Path is valid");
//it's valid. let the temporary turret to remain and relase the global object.
Destroy(GhostTurretG);
GhostTurretG = null;
//delete the grid from available grid's list.
listOfGridsAvailable.Remove(gridToCheck);
//destroy the grid.
Destroy(gridToCheck);
}
else
{
Debug.Log ("Path is not valid");
//it's invalid. destroy the temporary turret and let global object to remain.
Destroy(navMeshChecker);
}
lastGridMousePassedOver = null;
aGridSelected = false;
}
I'm trying now but, will it actually change anything? If so, then we will be found a bug in SetDestination, aren't we?
Edit: nothing changed, still same thing.
Your question is somewhat hard to follow. Can you strip down your code to something that succinctly demonstrates your problem?
Answer by starikcetin · Jun 05, 2015 at 11:45 PM
Allright, this solved the problem, temporarily... Cause I overload "WaitForEndOfFrame"s.
IEnumerator GhostComeAlive ()
{
aGridSelected = false;
//first create a temporary gameobject to check if path can complete itself.
GameObject gridToCheck = lastGridMousePassedOver;
GameObject navMeshChecker = Instantiate(GhostTurretG, gridToCheck.transform.position, gridToCheck.transform.rotation) as GameObject;
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
//calculate the path.
NavMeshPath path = new NavMeshPath();
// navMeshValidator.GetComponent<NavMeshAgent>().CalculatePath(navMeshTarget.transform.position, path);
//
// //now check if it's valid.
// if (path.status == NavMeshPathStatus.PathComplete)
NavMesh.CalculatePath(navMeshValidator.transform.position, navMeshTarget.transform.position, NavMesh.AllAreas, path);
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
if (path.status == NavMeshPathStatus.PathComplete)
{
Debug.Log ("Path is valid");
//it's valid. let the temporary turret to remain and relase the global object.
Destroy(GhostTurretG);
GhostTurretG = null;
//delete the grid from available grid's list.
listOfGridsAvailable.Remove(gridToCheck);
//destroy the grid.
Destroy(gridToCheck);
}
else
{
Debug.Log ("Path is not valid");
//it's invalid. destroy the temporary turret and let global object to remain.
Destroy(navMeshChecker);
}
lastGridMousePassedOver = null;
aGridSelected = false;
}
}
This code also caused the same problem now. Anyone has a solution out there? Or maybe i should change my game engine to unreal engine?
It seems like there is no solution for this. I must change to Unreal Engine before it's too late. Thanks all though.