How to use ICollisionEventsJob in Unity Physics 1.0.16?

  1. I couldn’t find Physics Shape. I think it was removed in 1.0.16? So I added a Sphere Collider to the GameObject.

  2. I wrote the script below.

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;


namespace Binggrae.CollisionSystem
{
    [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    [UpdateBefore(typeof(PhysicsSimulationGroup))]
    public partial struct CollisionSystem : ISystem
    {
        BufferLookup<CollisionBuffer> m_BufferLookup;

        public void OnCreate(ref SystemState state)
        {
            m_BufferLookup = state.GetBufferLookup<CollisionBuffer>(false);
        }

        public void OnUpdate(ref SystemState state)
        {
          
            var simulationSingleton = SystemAPI.GetSingleton<SimulationSingleton>();
            m_BufferLookup.Update(ref state);

            var colJobHandle = new CollisionJob
            {
                // ECB = ecb,
                collisionBuffers = m_BufferLookup

            }.Schedule(simulationSingleton, state.Dependency);
            colJobHandle.Complete();

        }



        private struct CollisionJob : ICollisionEventsJob
        {
            //public EntityCommandBuffer ECB;
            public BufferLookup<CollisionBuffer> collisionBuffers;

            public void Execute(CollisionEvent collisionEvent)
            {
                Debug.Log("Execute");
                if (collisionBuffers.HasBuffer(collisionEvent.EntityA))
                {
                    collisionBuffers[collisionEvent.EntityA].Add(new CollisionBuffer()
                    {
                        entity = collisionEvent.EntityB
                    });
                }

                if (collisionBuffers.HasBuffer(collisionEvent.EntityB))
                {
                    collisionBuffers[collisionEvent.EntityB].Add(new CollisionBuffer()
                    {
                        entity = collisionEvent.EntityA
                    });
                }


            }
        }
    }
}
  1. Debug.Log(“Execute”); is not called.
    Since it is colliding with the collider on the floor, Debug.log should be output. CollisionEvent is not being added to Entity even when Collider collides.

9465587--1329989--upload_2023-11-12_5-50-32.png

1 Like

You can still access the physics authoring component through the physics sample in the package manager.

You set your system to run before the physics simulation system group. But you need the simulation singleton to run your job.
Either you will run the collision even a frame late. Or they will have been forgotten by the time your reach your system. The physics system is stateless so there is no memory of collision from previous frame.

If that’s not it maybe you miss configure your layer overrides.

1 Like

Hi, I had the same problem. I was able to resolve it by importing Physics Shape and Physics Body to the project (1.0.16)

https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/custom-samples-physics-components.html
https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/concepts-simulation-set-up.html

and set Collision Response to Raise Events in Physics Shape