1
\$\begingroup\$

I'm trying to calculate the path to a point in my game using NavMesh.CalculatePath, it takes an argument NavMesh.AllAreas. I read the Unity Manual and couldn't clearly understand what it meant.

\$\endgroup\$
  • \$\begingroup\$ Any feedback is greatly appreciated. Don't hesitate to ask for more details. \$\endgroup\$ – Hellium Sep 4 '17 at 10:23
  • \$\begingroup\$ Nothing for now, will pose a question when I hit one, thanks for your answer. \$\endgroup\$ – azemda Sep 4 '17 at 10:56
4
\$\begingroup\$

TL;DR

NavMesh.AllAreas let you compute a path considering all the NavMesh Areas are walkable for your agent.


Long answer

Areas and costs

When you build a NavMesh in Unity, you have the possibility to divide it into multiple parts (called areas)

Each of these areas can have a different "cost" (for example, walking on the sand costs more efforts than walking on the grass).

enter image description here

Preventing agents to walk on certain areas

An other possibility is to prevent agents from walking on certain areas by specifying the 2nd argument when calling NavMesh.CalculatePath (if you have an enemy able to walk on the water but not another one for example). When specifying NavMesh.AllAreas, you allow your agent to find a path on every part of your NavMesh. In the Agent component, you can tell which areas he can walk on, using the AreaMask member.

AreaMask

What is areaMask and how do you compute it?

The value of areaMask when calling NavMesh.CalculatePath is a binary number. If you take a look at the first image, we have an array of areas. To indicate whether the character can walk on those areas, you have to specify 1 (walkable) or 0 (not walkable).

Supposing you agent can walk on every areas except "Non Walkable" and "Water"

  • Layer 0 : "Walkable" => 1
  • Layer 1 : "Not walkable" => 0
  • Layer 2 : "Jump" => 1
  • Layer 3 : "Water" => 0
  • Layer 4 : "Door" => 1

Result (from right to left) : 10101

  (1 << 0) + (0 << 1) + (1 << 2) + (0 << 3) + (1 << 4)
= 1 + 0 + 4 + 0 + 16
= 21 // Value to specify when calling NavMesh.CalculatePath 

Now, supposing you agent can walk on every areas except "Non Walkable"

  • Layer 0 : "Walkable" => 1
  • Layer 1 : "Not walkable" => 0
  • Layer 2 : "Jump" => 1
  • Layer 3 : "Water" => 1
  • Layer 4 : "Door" => 1

Result (from right to left) : 11101

  (1 << 0) + (0 << 1) + (1 << 2) + (1 << 3) + (1 << 4)
= 1 + 0 + 4 + 8 + 16
= 29 // Value to specify when calling NavMesh.CalculatePath 

The NavMesh class provides the GetAreaFromName function to help you computing the values above :

int mask = 0 ;
mask += 1 << NavMesh.GetAreaFromName("Walkable");
mask += 0 << NavMesh.GetAreaFromName("Not walkable");
mask += 1 << NavMesh.GetAreaFromName("Jump");
mask += 1 << NavMesh.GetAreaFromName("Water");
mask += 1 << NavMesh.GetAreaFromName("Door");

// Make sure you don't call multiple times the `GetAreaFromName` with the same area name.

And what about NavMesh.AllAreas?

The value of NavMesh.AllAreas is -1. Converted into the binary format, it is 11111111111111111111111111111111 (32 x 1 because an integer is written on 32 bits). So, if you specify 1 for every areas, you indicate all of them are walkable for your agent.

Source : https://docs.unity3d.com/Manual/nav-AreasAndCosts.html

| improve this answer | |
\$\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.