question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add flocking to navigation package

See original GitHub issue

Is the improvement related to a problem? Please describe.

I am trying to implement avoidance mechanics based on the current ECS-Sample repo by unity. However, I am not very successful. Agents either just drift away or stay on one position.

Describe the solution you’d prefer:

A flocking algorithm. I would like to implement enemy hordes which follow a target but avoid each other. They should continously aim to follow a target but once they bump into each other they should steer away from each other. This could be expanded to maybe play animations when they bump into each other.

Describe what you have tried:

I basically took the example from unity and modified it. I created a new hybrid agent type (NavAgentEnemy) and modified the example accordingly. Initial Jobs:

        `var initialCellAlignmentJobHandle = Entities
            .WithAll<NavEnemy>()
            .WithName("InitialCellAlignmentJob")
            .ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
            {
                cellAlignment[entityInQueryIndex] = localToWorld.Forward;
            })
            .ScheduleParallel(Dependency);

        var initialCellSeparationJobHandle = Entities
            .WithAll<NavEnemy>()
            .WithName("InitialCellSeparationJob")
            .ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
            {
                cellSeparation[entityInQueryIndex] = localToWorld.Position;
            })
            .ScheduleParallel(Dependency);

        var copyTargetPositionsJobHandle = Entities
            .WithName("CopyTargetPositionsJob")
            .WithNone<NavEnemy>()
            .WithAll<NavAgent>()
            .WithStoreEntityQueryInField(ref m_TargetQuery)
            .ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
            {
                copyTargetPositions[entityInQueryIndex] = localToWorld.Position;
            })
            .ScheduleParallel(Dependency);

        var copyObstaclePositionsJobHandle = Entities
            .WithName("CopyObstaclePositionsJob")
            .WithAll<EntityObstacle>()
            .WithStoreEntityQueryInField(ref m_ObstacleQuery)
            .ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
            {
                copyObstaclePositions[entityInQueryIndex] = localToWorld.Position;
            })
            .ScheduleParallel(Dependency);`

In the steer job the calculation basically remains the same I just used

var localToWorldFromEntity = GetComponentDataFromEntity<LocalToWorld>(true);

instead of ref LocalToWorld localToWorld.

The positions/ headings and general values seem to be correct, however I fail to apply it to my agent. I tried

var targetDestination = new float3(entityLocalToWorld.Position + (nextHeading * 6 * deltaTime));

commandBuffer.AddComponent<NavPlanning>(entityInQueryIndex,entity); commandBuffer.AddComponent(entityInQueryIndex, entity, new NavDestination { WorldPoint = targetDestination, Tolerance = 0, CustomLerp = false });

I also tried to set the translation.Value to the targetDestination.

I can not use localToWorldFromEntity[agent.DestinationSurface].Value, since agent.DestinationSurface = Entity.null.

Also setting the translation.Value directly bypasses the nav system so those agents aren’t finding valid paths anymore.

If I just add

commandBuffer.AddComponent(entityInQueryIndex, entity, new NavDestination { WorldPoint = nearestTargetPosition, Tolerance = 0, CustomLerp = false });

( nearestTargetPosition is the position (localToWorld.Position) of the nearest player ) the enemies will follow the player. But how can I get them to avoid each other and/ or add different behaviours like cohesion and alignment.

I am new to unity programming and dots especially and would be greatful even for a hint in the right direction.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
0x6c23commented, Jun 20, 2021

I have implemented a flocking prototype with waypoints, quadrants & NativeMultiHashMap, whithout using IJobNativeMultiHashMapMergedSharedKeyIndices.

If I rightly recall, however, modifying the NativeMultiHashMap, for when entities enter or exit a flock, would be tricky. The concurrent flavor of the data structure only supports adding new values, not removing them. So… that’s not gonna work.

I created a quadrantsystem with a static NativeMultiHashMap which gets cleared & rebuild every update. A flocking system calculates neighbors & steering vectors. A movement system constantly moves to a given point. The point consists of the waypoints (basically navlerpsystem) and added steering values.

It’s crude and a prototype but I could issue a pull request or alternatively add some code here for you to implement?

https://user-images.githubusercontent.com/32392595/122689508-55329a00-d223-11eb-8536-c75e9dd42d88.mp4

/E: This time, no waypoints. Just cohesion, alignment, separation and following.

https://user-images.githubusercontent.com/32392595/122691466-1eaf4c00-d230-11eb-94bf-8ca0aa452136.mp4

0reactions
0x6c23commented, Jun 22, 2021

I issued a pull request #70. Please check the code whenever you’re available.

I also added a new demo scene in Scenes > Nav > NavFlockingDemo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is flocking still a thing?
Ok, first I must admit that the unity navigation system is quite handy, but I am curious if flocking is still a thing....
Read more >
Flocking techniques to naturally support navigation in large ...
This paper proposes and explores the use of flocking techniques to naturally support navigation in large and open virtual environments.
Read more >
Clustering Methods in Flock Traffic Navigation Based on ...
In this research paper we are proposing new methods based on clustering in order to allow the entrance of new agents into the...
Read more >
O-Flocking: Optimized Flocking Model on Autonomous ...
We design the C-flocking model, which adds new obstacle avoidance strategies and directional movement strategy to the Reynolds' flocking model [ ...
Read more >
How To Repair Your Dash and Flock the Top!
Flocking, however, is as easy as applying a liquid adhesive, spraying on the flocking fibers, and letting the part dry for a day....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found