I have a situation in my app where I have a simple room, but it has two discrete states - one where the objects are laid out in one configuration, and one in another. The room switches at runtime between those states (although the player’s character can’t move during the transition.) I therefore have two NavMeshGraphs (modelled by hand, as they really are rather simple) which I switch at runtime.
My naive attempt to do this is to cache both graphs off in Start(), then when switching states call
Is this the correct way to handle this? I’m really looking just for a simple way to enable and disable a graph, and don’t need a detailed update framework (partly because the “update” is defined by a completely new mesh.) I’m on mobile, if it makes any difference.
This may not be the best answer… but I do what your asking this way:
I’ve hand modeled all of my navmesh graphs which I loaded one at a time into the pathfinder and saved a scanned copy. I changed the file extensions from .zip to .bytes. From script (C#) I have each one referenced as:
public TextAsset YourGraphName1;
public TextAsset YourGraphName2;
etc…
Since all the info I need is loaded at that point there is no reason to Scan again. Now my little people follow the new navmesh. I’m using this to represent all possible discrete states in a mobile game (many possible states) and doing it this way has no impact on my FPS. This is where I got the info to write this script: http://www.arongranberg.com/astar/docs/save-load-graphs.php
p.s. If you have save game functionality, call the one you want to start with (saved as a variable) during Start(); Otherwise, just cache startup in the pathfinder itself.
You can have both enabled at the same time and then let the AI choose which graph to use.
There is a nice parameter called “graphMask” which you can pass to the StartPath function on the Seeker. It will tell the pathfinder to only search the graphs which are enabled in the graphMask. So you can modify your AIPath script (or whatever you use for pathfinding:
`
public int graphMask = 1;
public void SearchPath (…) { //Or whatever this function was called
…
seeker.StartPath (transform.position, target.position, graphMask);
}`
Basically, when you want to change graph, you set graphMask = 1 << graphNumberIWantToUse;
Where graphNumberIWantToUse is the zero indexed… index of the graph. You would probably switch between 0 and 1 since you only have two graphs.
This switch is really fast, basically just the cost for assigning an int to a variable (practically nothing).