reeseschultz / ReeseUnityDemos

Unity packages and demos—emphasizing ECS, jobs and the Burst compiler—by Reese and others.

Home Page:https://reese.codes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nav: Add NavStop component

P-Stringer opened this issue · comments

commented

Hi @reeseschultz ,

I tweeted you just the other day regarding issues I'm having stopping an agent. Thought I'd start a discussion on here as it will be easier! So, in my project, I'm creating a couple of different component data structs to handle agent actions. For example:

public struct NavGoTo : IComponentData
{
     public float3 Location;
}

public struct NavStop : IComponentData
{
     // nothing here
}

public struct NavWander : IComponentData
{
     // nothing here
}

Then in my movement system I have

     // Handle NavWander
            Entities
                .WithAll<NavWander>()
                .WithNone<NavHasProblem, NavNeedsDestination, NavStop>()
                .WithReadOnly(renderBoundsFromEntity)
                .WithNativeDisableParallelForRestriction(randomArray)
                .ForEach((Entity entity, int entityInQueryIndex, int nativeThreadIndex, in Parent surface) =>
                {
                    if (
                        surface.Value.Equals(Entity.Null)
                    ) return;

                    var random = randomArray[nativeThreadIndex];
                    commandBuffer.AddComponent(entityInQueryIndex, entity, new NavNeedsDestination
                    {
                        Destination = NavTools.GetRandomPointInBounds(
                            ref random,
                            renderBoundsFromEntity[surface.Value].Value
                        )
                    });

                    randomArray[nativeThreadIndex] = random;
                })
                .WithName("NavWanderSystem")
                .ScheduleParallel();

            ... 
            
            // Handle NavStop 
            Entities
                .WithNone<NavHasProblem, NavWander>()
                .WithReadOnly(translationFromEntity)
                .WithNativeDisableParallelForRestriction(translationFromEntity)
                .ForEach((Entity entity, int entityInQueryIndex, in NavStop navStop) =>
                {
                    commandBuffer.RemoveComponent<NavStop>(entityInQueryIndex, entity);
                    commandBuffer.AddComponent(entityInQueryIndex, entity, new NavNeedsDestination
                    {
                        Destination = translationFromEntity[entity].Value
                    });
                    Debug.Log($"Stop entity ${entity.Index}");
                }).WithoutBurst()
                .WithName("NavStopSystem")
                .ScheduleParallel();

            Barrier.AddJobHandleForProducer(Dependency);

Ok, so this works - sometimes! Everytime I add a NavStop the system correctly picks this up and I see the DebugLog. However, the agent doesn't always stop. I've tried quite a few different things (been at this simple stop functionality for about 4 days! lol) and while I'm new to ECS, I can't for the life of me figure out why what I'm doing shouldn't work... the fact it sometimes works makes it even more frustrating. I'm wondering if it could be something to do with the way NavNeedsDestination works and so my next step was to ask you for some advice. Can you think of any reason why setting the Destination to the entities Translation won't work?

Just to note, I've also used the Translation in the ForEach lambda rather using var translationFromEntity = GetComponentDataFromEntity<Translation>(true); but get the same result.

Any thoughts? Would really appreciate any insight you might have.

Thanks

Edit: Maybe it's because I'm using .ScheduleParallel() and they are conflicting? It appears as though .Schedule improves things slightly but there is still and issue.

This is a good issue because I'll need this soon for a project, along with a couple other components. Lots of new stuff in store.

For NavStop to work right, it would need to remove any possible nav-related components other than NavAgent. These are all in here. That said, I think it only makes sense for me to implement NavStop in the nav library itself, rather than requiring users such as yourself to do this. I can probably make this change later today or tomorrow.

With the way I'll implement it, you should be able to use NavStop just as you are.


Side note: I'd have to do some investigation to find out why your solution doesn't work, although what you have is more of a workaround since I'm not giving you a good way to accomplish what you really want to do at the moment. In the feat/demo-overhaul branch, navigation was updated to support mousedrag / agent follow functionality, and I'd bet your code would work better because of that; however, it's out of sync with master. I'm going to set aside time, hopefully next weekend, to deal with that.

There's other stuff I've been asked about on Discord that really needs to get implemented as well, such as exposing dependencies from the nav systems.

commented

Lots of new stuff in store.

Great!

Ok, I think for now then I'll focus my attention else where and hang fire with the NavStop. Let me know once you've found some time to add it :) In the meantime, makes sense for me to have a mess with the demo-overhaul branch as it sounds like there are a few things over there that will help me out - big one being agent follow.

Totally forgot about Discord - will probably see you over there. Cheers!

@P-Stringer I added NavStop, which is part of Nav v0.15.0. You can view the changes in this commit. It should work. I tested it in the NavHybridDemo by making the agent stop when Tab is pressed (sometimes the agent's animation freezes—that's a separate, scene-specific issue). Anyway, I really appreciate that you brought this up, and also that you tried to apply a workaround and shared the code for what you did (most people don't). I was able to knock this out this morning since I don't have much time later today.

Be aware that the feat/demo-overhaul has not been synced with master, meaning it's missing later changes that that were added to master. Like I said, I'll try to sync them, but it'll be at least a week before I can do that. There's no NavFollow component in that branch (yet!), but the underlying code should now be ready to support it, is what I mean. I know it should be about ready because the new "Stranded" demo supports click-and-drag movement.

commented

Works perfectly! Thanks so much - spent too much time on this and finally feel like I can move on 😄

Glad it helps!