- Home /
How NavMesh Cost can slow down NavMeshAgent's speed?
Hi I have a terrain mesh and a water mesh in my scene. I make them in the different layer for Navigation. Terrain mesh in the "default" layer and water mesh in the "water" layer. I set the "water" layer's cost to 100. What I want is the "water" layer still can walk through but it should make NavMeshAgent walk slowly if they have no choice need to go through the "water" area. But I dont know how to implement this. How can I get the NavMeshAgent's layer cost for current position? How to determine the NavMeshAgent already in the "water" area?
Answer by Happy-Zomby · Aug 03, 2015 at 11:48 AM
Hello, I found an answer (thank you to poster!) to a similar problem to the one in this old post somewhere but can't seem to put my hand on the hyperlink... anyway - you can sample the area where your unit is
var agent : NavMeshAgent;
function Start ()
{
agent = GetComponent(NavMeshAgent);
}
function Update ()
{
var navHit : NavMeshHit;
agent.SamplePathPosition (-1, 0.0f, navHit);
Debug.Log(navHit.mask);
}
you will get a bitfield result (Built-in areas: 1, 2, 4, user added areas : 8, 16, 32, 64, 128....) which you can use to identify your area index (0, 1, 2, 3 ....) and therefore your area cost / speed / event, etc... hope this helps if you stumble over this old post,
Answer by nventimiglia · Jul 04, 2012 at 12:53 PM
Create trigger areas to handle the speed modifier
[RequiredComponenet(typeof(Collider))]
public class TerrainTrigger : MonoBehaviour{
public float SpeedChange = -100;
OnTriggerEnter(collider Other){
other.SendMessage("SpeedChange",SpeedChange);
}
OnTriggerExit(collider other){
other.SendMessage("SpeedChange",-SpeedChange);
}
}
Answer by Sirithang · Jul 20, 2012 at 12:29 PM
What about using http://docs.unity3d.com/Documentation/ScriptReference/NavMeshAgent.FindClosestEdge.html
It find the closest Edge from the Agent, and return usefull information in the out parameter (including the mask composed of layers for this edge).
So this should work (untested!):
NavMeshHit lHit = new NavMeshHit();
if(FindClosestEdge(out lHit)
{
if(lHit.mask & **N**) // where N is the number of the layer to test
{
//do here what you'll do for this layer
}
}
Your answer
Follow this Question
Related Questions
How NavMesh Cost can slow down NavMeshAgent's speed? 0 Answers
Why navmesh area cost is not changing agent's path? 0 Answers
NavMeshAgents bumping into stationary agents 1 Answer
Can't get NavMeshAgent move. NavMeshAgent ignores destination property 7 Answers
Navmesh Path is shown even between disconnected regions.? 0 Answers