Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to use ICollisionEventsJob in Unity Physics 1.0.16?

Discussion in 'Physics for ECS' started by mrbinggrae, Nov 11, 2023.

  1. mrbinggrae

    mrbinggrae

    Joined:
    Mar 6, 2016
    Posts:
    22
    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.
    upload_2023-11-12_5-50-56.png

    2. I wrote the script below.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Entities;
    4. using Unity.Physics;
    5. using Unity.Physics.Systems;
    6. using UnityEngine;
    7.  
    8.  
    9. namespace Binggrae.CollisionSystem
    10. {
    11.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    12.     [UpdateBefore(typeof(PhysicsSimulationGroup))]
    13.     public partial struct CollisionSystem : ISystem
    14.     {
    15.         BufferLookup<CollisionBuffer> m_BufferLookup;
    16.  
    17.         public void OnCreate(ref SystemState state)
    18.         {
    19.             m_BufferLookup = state.GetBufferLookup<CollisionBuffer>(false);
    20.         }
    21.  
    22.         public void OnUpdate(ref SystemState state)
    23.         {
    24.          
    25.             var simulationSingleton = SystemAPI.GetSingleton<SimulationSingleton>();
    26.             m_BufferLookup.Update(ref state);
    27.  
    28.             var colJobHandle = new CollisionJob
    29.             {
    30.                 // ECB = ecb,
    31.                 collisionBuffers = m_BufferLookup
    32.  
    33.             }.Schedule(simulationSingleton, state.Dependency);
    34.             colJobHandle.Complete();
    35.  
    36.         }
    37.  
    38.  
    39.  
    40.         private struct CollisionJob : ICollisionEventsJob
    41.         {
    42.             //public EntityCommandBuffer ECB;
    43.             public BufferLookup<CollisionBuffer> collisionBuffers;
    44.  
    45.             public void Execute(CollisionEvent collisionEvent)
    46.             {
    47.                 Debug.Log("Execute");
    48.                 if (collisionBuffers.HasBuffer(collisionEvent.EntityA))
    49.                 {
    50.                     collisionBuffers[collisionEvent.EntityA].Add(new CollisionBuffer()
    51.                     {
    52.                         entity = collisionEvent.EntityB
    53.                     });
    54.                 }
    55.  
    56.                 if (collisionBuffers.HasBuffer(collisionEvent.EntityB))
    57.                 {
    58.                     collisionBuffers[collisionEvent.EntityB].Add(new CollisionBuffer()
    59.                     {
    60.                         entity = collisionEvent.EntityA
    61.                     });
    62.                 }
    63.  
    64.  
    65.             }
    66.         }
    67.     }
    68. }
    69.  
    70.  
    3. 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.
     

    Attached Files:

  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    974
    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.
     
    mrbinggrae likes this.
  3. borysovcorp

    borysovcorp

    Joined:
    Nov 25, 2019
    Posts:
    4