0
\$\begingroup\$

I'm using NavMesh, and NavMeshAgent for my character movement and Pathfinding.

I made a testing NavMesh with 2 area layers:

0 = Walkable 3 = Water.

In have a click to move script, and i need to get the ID of the NavMesh Area where the player clicked. I tried to get the ID of the layer where the player clicked, using NavMeshHit.mask but is always resulting 0, even if I click where the NavMesh area ID is 3.

From Unity documentation:

Mask specifying NavMesh area at point of hit.

Script:

RaycastHit hit;
NavMeshHit nHit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
    bool hasHit = NavMesh.Raycast (this.transform.position, ray.direction, out nHit, NavMesh.AllAreas);
    if (hasHit) {
        Debug.Log ("Area ID: " + nHit.mask);
        targetPosition = hit.point;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Which object is this script attached to? \$\endgroup\$
    – LVBen
    Commented Sep 13, 2015 at 18:34
  • \$\begingroup\$ It attached to my character. \$\endgroup\$
    – Fr0z3n
    Commented Sep 13, 2015 at 18:39
  • \$\begingroup\$ The raycast is going out of the camera and you are checking another raycast from player position. If the camera is even slightly away from the character - it can give you totally different hit point. \$\endgroup\$ Commented Sep 17, 2017 at 19:40

2 Answers 2

0
\$\begingroup\$

Your 2nd argument appears to be incorrect. The 2nd argument should be the point in which the ray intersected with the object that was clicked on.

Like this:

RaycastHit hit;
NavMeshHit nHit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
    bool hasHit = NavMesh.Raycast (this.transform.position, hit.point, out nHit, NavMesh.AllAreas);
    if (hasHit) {
        Debug.Log ("Area ID: " + nHit.mask);
        targetPosition = hit.point;
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ It's not working, eve with a simple Debug.DrawLine() the ray starts at the camera Position, and hit the wrong position. Doesn't matter where i click, the Ray hits the same position. So there must be something wrong. And yes, the NavMesh.Raycast must match the Physic Raycast. \$\endgroup\$
    – Fr0z3n
    Commented Sep 13, 2015 at 18:56
  • \$\begingroup\$ docs.unity3d.com/ScriptReference/NavMesh.Raycast.html I think that the second parameter is wrong, but if i use the hit.position the raycast show correctly, but doesnt return true. \$\endgroup\$
    – Fr0z3n
    Commented Sep 13, 2015 at 18:59
  • \$\begingroup\$ It should start at the camera position. The direction should be towards the object that was clicked on. \$\endgroup\$
    – LVBen
    Commented Sep 13, 2015 at 18:59
  • \$\begingroup\$ oh, you are probably right. \$\endgroup\$
    – LVBen
    Commented Sep 13, 2015 at 19:01
  • \$\begingroup\$ I updated my answer. You want to use the point that the ray hit as the 2nd argument and the character(or navAgents)'s position as the 1st argument. \$\endgroup\$
    – LVBen
    Commented Sep 13, 2015 at 19:06
0
\$\begingroup\$

Documenting the SamplePosition solution from Fr0z3n's comment to give this Q&A thread some closure:

I found the problem. I need to use NavMesh.SamplePosition() instead of Raycast()

Code implementing this fix could look like this:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out var hit)) {
    const float checkRadius = 1.0f;
    if (NavMesh.SamplePosition(hit.point, out var navHit, checkRadius, NavMesh.AllAreas)) {
        Debug.Log ($"Area ID: {navHit.mask}");
        if (navHit.mask & walkableMask != 0) {
            targetPosition = hit.point;
        }
    } else {
        Debug.LogError($"No navmesh found near clicked point: {hit.point}");
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .