- Home /
DOTS trigger event is firing twice?
I'm trying to create a simple system that will delete any entity that comes in contact with an entity that has the "bounds" tag using current ECS best practices in unity 2020.2.0b12, and it seems that the entities are not being deleted until an update after the first trigger, leading the trigger to happen twice when it should only happen once.
It does work as intended (the entity is deleted), but I'm confused as to why I'm seeing two collisions rather than just one. I created an empty project, installed com.unity.physics and com.unity.rendering.hybrid and enabled the V2 hybrid renderer. There are 2 scripts, BoundsTag.cs:
[Serializable]
[GenerateAuthoringComponent]
public struct BoundsTag : IComponentData
{ }
BoundsSystem.cs:
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public class BoundsSystem : SystemBase
{
struct BoundsSystemJob : ITriggerEventsJob
{
[ReadOnly] public ComponentDataFromEntity<BoundsTag> allBounds;
public EntityCommandBuffer entityCommandBuffer;
public void Execute(TriggerEvent triggerEvent)
{
var outOfBoundsEnt = allBounds.HasComponent(triggerEvent.EntityA) ? triggerEvent.EntityB : triggerEvent.EntityA;
entityCommandBuffer.DestroyEntity(outOfBoundsEnt);
Debug.LogFormat("Poof! {0} {1} {2} {3} {4} {5} {6}", outOfBoundsEnt, triggerEvent.EntityA, triggerEvent.EntityB, triggerEvent.ColliderKeyA, triggerEvent.ColliderKeyB, triggerEvent.BodyIndexA, triggerEvent.BodyIndexB);
}
}
protected override void OnUpdate()
{
Debug.LogFormat("Update");
var job = new BoundsSystemJob();
var cbs = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
job.allBounds = GetComponentDataFromEntity<BoundsTag>(true);
job.entityCommandBuffer = cbs.CreateCommandBuffer();
this.Dependency = job.Schedule(World.GetOrCreateSystem<StepPhysicsWorld>().Simulation, ref World.GetOrCreateSystem<BuildPhysicsWorld>().PhysicsWorld, this.Dependency);
cbs.AddJobHandleForProducer(this.Dependency);
}
}
The large cube has BoundsTag, Physics Shape (with Collision response set to Raise Triggers), and ConvertToEntity, the ball has Physics Shape, Physics Body, and ConvertToEntity (but not BoundsTag). I'd expect it to fall into the cube, emit one instance of the 'Poof!' log message, and then disappear. It does fall into the cube and disappear, but the Poof! message happens twice. The output is:
Update
Update
...
Poof! Entity(1:1) Entity(1:1) Entity(0:1) ColliderKey { Value = 4294967295 } ColliderKey { Value = 4294967295 } 0 1
Update
Poof! Entity(1:1) Entity(1:1) Entity(0:1) ColliderKey { Value = 4294967295 } ColliderKey { Value = 4294967295 } 0 1
Update
...
it doesn't really matter for this toy example, but it will definitely cause issues when I'm doing more complex logic. If there's a procedure for attaching a project folder to this question, I can do that.
I've tried a few variants of the UpdateInGroup clause (including not having it at all) and different CommandBufferSystems, but nothing seems to change.
Did calling this.Dependency.Complete();
before scheduling a new job changed anything?
Nope. Neither did calling it before returning, or both.