#NavMeshQuery and Jobs

1 messages · Page 1 of 1 (latest)

sleek pine
#

Hello!
I'm trying to make a simple job to handle nav meshes via NavMeshQuery.

What's the right way to handle this? When creating a new NavMeshQuery, I must pass a NavMeshWorld but fetching a world with GetDefaultWorld results in

GetDefaultWorld can only be called from the main thread.

and if I fetch the default world from the main thread and pass it as an arg, I get:

world uses unsafe Pointers which is not allowed.

And even if I use [NativeDisableUnsafePtrRestriction] on the param, I still get the same unsafe pointers error.

What gives? What is the right way to use this?

Here's what my code currently looks like:

public class NavigationScheduler : MonoBehaviour
{
    public Transform Origin;
    public Transform Target;

    private JobHandle _handle;

    private void Awake()
    { }

    private void Start()
    {
        int index = 0;
        NavMeshWorld w = NavMeshWorld.GetDefaultWorld();
        JobNav j = new(Origin.position, Target.position, 0, w);

        _handle = j.Schedule();

        StartCoroutine(Routine());
    }

    private IEnumerator Routine()
    {
        yield return new WaitForSeconds(1f);

        _handle.Complete();
    }

    private void OnDestroy()
    { }
}




public struct JobNav : IJob
{
    private float3 _startPos;
    private float3 _endPos;
    private int _agentId;
    private NavMeshWorld _world;

    public JobNav(float3 startPosition, float3 endPosition, int agentId, NavMeshWorld w)
    {
        _startPos = startPosition;
        _endPos = endPosition;
        _agentId = agentId;
        _world = w;
    }

    public void Execute()
    {
        NavMeshQuery q = new NavMeshQuery(_world, Allocator.Persistent, 30000);

        Vector3 extents = Vector3.one;
        NavMeshLocation ls = q.MapLocation(_startPos, extents, _agentId);
        NavMeshLocation le = q.MapLocation(_endPos, extents, _agentId);

        q.BeginFindPath(ls, le);

        //q.UpdateFindPath()
    }
}
noble coyote
#

You have to create the NavMeshQuery on the main thread as well.

#

And pass in the constricted NavMeshQuery to the job