Off Mesh Link - Limiting access to certain agents
In my game I have dynamically created Nav Mesh agents that wander around the map. There are doors on the map which I am using Off Mesh Links for. Is there a way I could have these off mesh links only accessible to certain agents. Is this possible? Do I need to use an Obstacle?
Any help is greatly appreciated.
Answer by CraigGraff · Nov 25, 2015 at 10:05 AM
@2-Zons You can just set the navigation area of the Off Mesh links to some custom navigation areas (added in the Areas tab of the Navigation panel). Then use something like the following code to set the area mask of each agent that is allowed to travel through a gate.
using UnityEngine;
public enum Gate {Red=3, Green, Blue}
public class AgentAreaMaskExample : MonoBehaviour
{
NavMeshAgent agent;
void setGateAllowed(Gate gate, bool allowed)
{
if (allowed)
// set bit on
agent.areaMask |= 1 << (int)gate;
else
// set bit off
agent.areaMask &= ~(1 << (int)gate);
}
void Start()
{
agent = GetComponent<NavMeshAgent>();
setGateAllowed(Gate.Green, true);
}
}
Thank you for the reply. Can those areas be dynamically assigned at runtime? The doors I am using are placed by the player.
@2-Zons Yes, with something like:
Off$$anonymous$$eshLink link = GetComponent<Off$$anonymous$$eshLink>();
link.area = 3; // set link to only allow characters if they can traverse area 3
This looks like it will do the trick. As soon as I can get it implemented in my project and test it I will accept this answer as correct. There seem to be a lot of people following this question, so I don't want to accept the answer until I am certain it works as desired.
Answer by 2-Zons · Nov 27, 2015 at 04:01 AM
This worked just as described. One thing that I needed to be careful with was the doors are prefab's and and the enter and exit tied to child game objects. The doors needed placed so that the enter and exit transforms are within a certain tolerance of the nav mesh surface. if placed too high they wont function properly. Thanks @CraigGraff
Your answer
Follow this Question
Related Questions
Navigation 0 Answers
Farthest Reachable Destination 0 Answers
Networked movement using navmesh agent 0 Answers
Navmesh agent passing through obstacles (path goes under navmesh) 1 Answer