[ad_1]
Update: This reply was initially written based mostly on Version 0.51 of the physics system. The documentation of model 1.0 of the physics system has a chapter on processing collision detection occasions that exhibits an answer making use of a few of the new idioms. That article may be extra useful than this reply.
You can do that with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You additionally have to set that job as a dependency for the FinishSimulationEntityCommandBufferSystem utilizing AddJobHandleForProducer. Here is an instance script:
utilizing Unity.Burst;
utilizing Unity.Entities;
utilizing Unity.Jobs;
utilizing Unity.Physics;
utilizing Unity.Physics.Systems;
utilizing UnityEngine;
public class CollisionSystem : JobComponentSystem {
[BurstCompile]
personal struct CollisionJob : ICollisionEventsJob {
public void Execute(CollisionOccasion collisionEvent) {
Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
}
}
personal ConstructPhysicsWorld constructPhysicsWorldSystem;
personal StepPhysicsWorld stepPhysicsWorldSystem;
personal FinishSimulationEntityCommandBufferSystem commandBufferSystem;
protected override void OnCreate() {
base.OnCreate();
constructPhysicsWorldSystem = World.GetExistingSystem<ConstructPhysicsWorld>();
stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
commandBufferSystem = World.GetExistingSystem<FinishSimulationEntityCommandBufferSystem>();
}
protected override JobHandle OnUpdate(JobHandle inputDeps) {
JobHandle jobHandle = new CollisionJob().Schedule(
stepPhysicsWorldSystem.Simulation,
ref constructPhysicsWorldSystem.PhysicsWorld,
inputDeps);
commandBufferSystem.AddJobHandleForProducer(jobHandle);
return jobHandle;
}
}
Note that the physics system will often generate a number of collision occasions on what seems to be only a single collision to the participant. There is some debate about whether or not that is a bug or a characteristic.
* Yes, I’m conscious how ineffective this documentation article is true now. Hopefully it’ll get extra helpful in future variations of the documentation.
[ad_2]