Forum rules - please read before posting.

Unity pathfinding and Mesh Colliders (v1.69.5)

edited December 2019 in Technical Q&A

I am building a 3D game with Unity pathfinding. To have more control over the generated navmesh, I have disabled the "Navigation static" property from my scene geometry and bake the mesh out of Navmesh Segments.

The Navmesh Segments are generated on layer "Ignore Raycast" and I have enabled "Navigation Static" on them to make Unity use them for navigation mesh baking.

The colliders on the scene geometry still exist for character controller logic and footstep detection.

Unfortunately, this causes the "mouse click" result to be far off sometimes. It worked quite well as long as my floor consisted of square tiles. But now I've added colliders to carpets, to detect different materials for footstep sounds, and as they're lying quite unordered on the ground and come in different forms, the calculated movement destination is no longer acceptable.

I inspected PlayerMovement.cs and found out that the "Move Ray down screen until we hit something" logic (line 899+) does 39 iterations (the "NavMesh search direction" logic) until it finds something, and I was wondering why this is the case if all it should do is look at a simple navmesh.

I noticed that line 1094+ does this:

                if (KickStarter.settingsManager && KickStarter.sceneSettings && Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength))
                {
                    return ProcessHit (hit.point, hit.collider.gameObject, run);
                }

and then proceeds with this in ProcessHit:

            if (hitObject.layer != LayerMask.NameToLayer (KickStarter.settingsManager.navMeshLayer))
            {
                return false;
            }

If you proceed only with colliders on the navMeshLayer anyway, why don't you filter the Raycast before accordingly? Shouldn't that speed up matters?

I changed the line 1094 to read:

if (KickStarter.settingsManager && KickStarter.sceneSettings && Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength, 1 << LayerMask.NameToLayer(KickStarter.settingsManager.navMeshLayer)))

and now it works for me as I would expect it, with the proper collision object on the relevant layer found immediately.

Is this a bug in AC or did I miss anything?

Thanks
Matt

Comments

  • Welcome to the community, @digitalbreed.

    I think I'd need some screenshots that illustrate the issue to help me understand the exact situation, but likely this is down to the fact that non-NavMesh colliders are (as intended) used to block mouse-clicks. This is so that e.g. clicks on walls that hide other rooms are ignored.

    If you could, it perhaps would be easier to rely on the same collider to act as both the clickable NavMesh area, and the source for footstep detection - the latter could rely on Tags instead of Layers to indicate material.

    Otherwise, you could try raising the NavMesh collider ever-so-slightly above the floor, so that clicks upon it always detect.

  • edited December 2019

    @ChrisIceBox, thanks for your response. I hope you had a couple of joyful festive days!

    Here are screenshots and videos to illustrate the issue.

    First, I set up the walkable area using navmesh segments. I did this mainly to have full control over where the character can walk, in particular with regards to doors (I think there's a corresponding sentence in the AC Manual, too). You can see the navmesh segments shine through here:

    Note that the carpet area is also covered, it's just not showing as the carpets are "hovering" a little above ground.

    As mentioned earlier, all the geometry has "Navigation Static" unticked but the navmesh segments have it ticked.

    The corresponding baked navmesh is shown here:

    So far, navigation worked nicely, as shown in this video:

    https://www.dropbox.com/s/sdddupia7bz1ilu/advcreator-carpet-meshcollider-disabled.mp4?dl=0 (1 MB MP4)

    With a mesh collider attached to the carpet, it becomes impossible to position the player right on the carpet for the reasons described in my posting above. It's hard to see because I've not yet implemented any visual feedback for clicks or navigation targets, but I try to make the player walk right upon the carpet.

    https://www.dropbox.com/s/ca7azh19m36firo/advcreator-carpet-meshcollider-enabled.mp4?dl=0 (1 MB MP4)

    Now that I am aware of how the internals of AC look like, it occurs to me that even the accuracy in the first video is just a perceived one. The navigation logic keeps iterating until the raycast returns one of the navmesh segments...

    Is there any way to keep control with own navmesh segments, accurately navigate the player through the scene, and be able to raycast for footstep sound detection?

    Thanks
    Matt

  • Otherwise, you could try raising the NavMesh collider ever-so-slightly above the floor, so that clicks upon it always detect.

    I tried that by clicking on the generated NavMesh asset for the scene and changing the Position Y field in the inspector, but I didn't see any improvement. Interestingly, I also didn't see it moving in the navmesh preview regardless of any translation or rotation changes I made.

  • The behaviour is as-intended - there should be a clear path between the camera and the areas of the NavMesh you want to be clickable. This gives you control about limiting access to e.g. other rooms.

    AC's own footstep sound system works by using Trigger boundaries to call Sound: Change footstep Actions. This involves placing down colliders that the character walks through, rather than raycasting downwards.

    If your method involves raycasting, is it from another asset or your own scripting? It should be possible to change that system's behaviour instead, so that those Raycasts ignore AC's NavMesh layer. If you'd like to share more details on exactly how it currently works, I may be able to offer more specific advice.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.