speed by area cost

Hi I have setup a simple navmesh demo where the player moves to the klicked destination.

I use this simple line of code for the navigation

agent.SetDestination(hit.point);

I have added a new navigation area with higher costs.
The pathfinding works perfectly but while moving the NavMeshAgent don’t changes speed.

I want that you can see that it is slower to walk through this area.

Is there a simple way to achieve this?

Hi,

The simplest solution is trigger collider OnEnter and OnExit.
The other way is NavMesh Raycast or SamplePosition, onHit it will contain area info to tweak speed.

1 Like

Can I use the NavMeshHit.mask for the agent.GetAreaCost()?
Because I have my custom area at index 3 but the NavMeshHit.mask is 8.
My used code is:

NavMeshHit navhit;
NavMesh.SamplePosition(transform.position, out navhit, 10,NavMesh.AllAreas);
Debug.Log(navhit.mask);

Hi,

Area mask 0x08 in decimal equals to 8, its Area 4th by index, but we count from zero so its area cost is agent.GetAreaCost(3). Mask shows 1 in binary form to denote area index

5649199--587410--upload_2020-3-30_19-44-45.png

        public static int IndexFromMask(int mask)
        {
            for (int i = 0; i < 32; ++i)
            {
                if ((1 << i & mask) != 0)
                {
                    return i;
                }
            }
            return -1;
        }
1 Like