Skip to main content
fixed broken link
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Update: This answer was originally written based on Version 0.51 of the physics system. The documentation of version 1.0 of the physics system has a chapter on processing collision detection eventsa chapter on processing collision detection events that shows a solution applying some of the new idioms. That article might be more helpful than this answer.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

Update: This answer was originally written based on Version 0.51 of the physics system. The documentation of version 1.0 of the physics system has a chapter on processing collision detection events that shows a solution applying some of the new idioms. That article might be more helpful than this answer.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

Update: This answer was originally written based on Version 0.51 of the physics system. The documentation of version 1.0 of the physics system has a chapter on processing collision detection events that shows a solution applying some of the new idioms. That article might be more helpful than this answer.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

added 415 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Update: This answer was originally written based on Version 0.51 of the physics system. The documentation of version 1.0 of the physics system has a chapter on processing collision detection events that shows a solution applying some of the new idioms. That article might be more helpful than this answer.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

Update: This answer was originally written based on Version 0.51 of the physics system. The documentation of version 1.0 of the physics system has a chapter on processing collision detection events that shows a solution applying some of the new idioms. That article might be more helpful than this answer.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

added 1 character in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

You can do this with a JobComponentSystem*JobComponentSystem which schedules a job implementing ICollisionEventJobICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless thosethis documentation articles arearticle is right now. Hopefully theyit will get more useful in future versions of the documentation.

You can do this with a JobComponentSystem* which schedules a job implementing ICollisionEventJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless those documentation articles are right now. Hopefully they will get more useful in future versions of the documentation.

You can do this with a JobComponentSystem which schedules a job implementing ICollisionEventsJob*. You also need to set that job as a dependency for the EndSimulationEntityCommandBufferSystem using AddJobHandleForProducer. Here is an example script:

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

public class CollisionSystem : JobComponentSystem {

    [BurstCompile]
    private struct CollisionJob : ICollisionEventsJob {
        public void Execute(CollisionEvent collisionEvent) {
            Debug.Log($"Collision between entities { collisionEvent.EntityA.Index } and { collisionEvent.EntityB.Index }");
        }
    }

    private BuildPhysicsWorld buildPhysicsWorldSystem;
    private StepPhysicsWorld stepPhysicsWorldSystem;
    private EndSimulationEntityCommandBufferSystem commandBufferSystem;

    protected override void OnCreate() {
        base.OnCreate();
        buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.GetExistingSystem<StepPhysicsWorld>();
        commandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps) {    
        JobHandle jobHandle = new CollisionJob().Schedule(
            stepPhysicsWorldSystem.Simulation, 
            ref buildPhysicsWorldSystem.PhysicsWorld, 
            inputDeps);    
        commandBufferSystem.AddJobHandleForProducer(jobHandle);    
        return jobHandle;    
    }
}

Note that the physics system will usually generate multiple collision events on what appears to be just a single collision to the player. There is some debate about whether that's a bug or a feature.

* Yes, I am aware how useless this documentation article is right now. Hopefully it will get more useful in future versions of the documentation.

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading