Add flocking to navigation package
See original GitHub issueIs 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:
- Created 2 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
I have implemented a flocking prototype with waypoints, quadrants & NativeMultiHashMap, whithout using IJobNativeMultiHashMapMergedSharedKeyIndices.
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
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.