2
\$\begingroup\$

I'm writing a tower defense game in Unity, inspired by Desktop Tower Defense.

Creeps spawn on one side of the screen, and if they make it to the opposite side, the player takes damage. The board starts out unobstructed, and the player builds towers that shoot at creeps while also blocking their path. They can funnel creeps along a path by strategic placement of towers.

I have a terrain GameObject with a NavMesh, each creep has a NavMeshAgent, and each tower has a NavMeshObstacle. This works fine, the creeps go toward the goal and avoid the towers.

The problem is that the player is not allowed to block off all of the paths to the goal. If placing a tower would leave no routes open to the goal, it is not valid to place a tower there. I can detect this after the tower is placed, but I want to warn the player and prevent the placement from happening at all.

I also don't want to even briefly block the path to the goal by temporarily placing a NavMeshObstacle in an invalid position, because it might be exploitable by players by repeatedly attempting to place a tower there.

How can I predict whether placing a NavMeshObstacle will completely block a route between two paths, while NavMeshAgents are actively traversing the NavMesh?

\$\endgroup\$
  • \$\begingroup\$ Possible idea: maintain a second navmesh that is a duplicate of the real navmesh, and place towers there first. \$\endgroup\$ – error Jul 18 '17 at 3:38
1
\$\begingroup\$

I would add something like a "temporary" wall on the place where the player want to build. Meanwhile on a other navmesh an Object is moving all the time super fast (something like a 1x1 collider). If this object didn't finish the route, that will activate a script which disable the option to build there.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.