Search Unity

Question You are trying to schedule a new job CollisionSystem:CollisionSystemJob, which reads from the same U

Discussion in 'Physics for ECS' started by ErezShahaf, Aug 2, 2022.

  1. ErezShahaf

    ErezShahaf

    Joined:
    Feb 3, 2022
    Posts:
    70
    I'm trying to store collisions inside a buffer for entities that implement my CollisionBuffer:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Physics;
    4. using Unity.Physics.Systems;
    5.  
    6. [UpdateAfter(typeof(ExportPhysicsWorld))]
    7. public partial class CollisionSystem : SystemBase
    8. {
    9.  
    10.  
    11.     private struct CollisionSystemJob : ICollisionEventsJob
    12.     {
    13.         public BufferFromEntity<CollisionBuffer> collisions;
    14.         public void Execute(CollisionEvent collisionEvent)
    15.         {
    16.             if (collisions.HasComponent(collisionEvent.EntityA))
    17.             {
    18.                 collisions[collisionEvent.EntityA].Add(new CollisionBuffer() { entity = collisionEvent.EntityB });
    19. }
    20.             if (collisions.HasComponent(collisionEvent.EntityB))
    21.             {
    22.                 collisions[collisionEvent.EntityB].Add(new CollisionBuffer() { entity = collisionEvent.EntityA });
    23.             }
    24.         }
    25.     }
    26.  
    27.     protected override void OnUpdate()
    28.     {
    29.         var pw = World.GetOrCreateSystem<BuildPhysicsWorld>().PhysicsWorld;
    30.         var sim = World.GetOrCreateSystem<StepPhysicsWorld>().Simulation;
    31.  
    32.         Entities.ForEach((DynamicBuffer<CollisionBuffer> collisions) =>
    33.         {
    34.             collisions.Clear();
    35.         }).Run();
    36.  
    37.         JobHandle colJobHandle = new CollisionSystemJob()
    38.         {
    39.             collisions = GetBufferFromEntity<CollisionBuffer>()
    40.         }.Schedule(sim, this.Dependency);
    41.         colJobHandle.Complete();
    42.     }
    43. }
    44.  
    45.  
    Here's the error that I'm getting:
    Code (CSharp):
    1.  The previously scheduled job Solver:ParallelApplyGravityAndCopyInputVelocitiesJob writes to the Unity.Collections.NativeArray`1[Unity.Physics.Velocity] ParallelApplyGravityAndCopyInputVelocitiesJob.InputVelocities. You are trying to schedule a new job CollisionSystem:CollisionSystemJob, which reads from the same Unity.Collections.NativeArray`1[Unity.Physics.Velocity] (via CollisionSystemJob.EventReader.m_InputVelocities). To guarantee safety, you must include Solver:ParallelApplyGravityAndCopyInputVelocitiesJob as a dependency of the newly scheduled job.
    I have been googling this issue and it seems like for some people [UpdateAfter(typeof(ExportPhysicsWorld))] managed to solve it. But it isn't true in my case.

    While we are at it, why doesn't unity have a system similar to this one inside their physics package? it seems very basic.
     
  2. ErezShahaf

    ErezShahaf

    Joined:
    Feb 3, 2022
    Posts:
    70
    Right now I'm using the attributes:

    [UpdateAfter(typeof(ExportPhysicsWorld))]
    [UpdateBefore(typeof(EndFramePhysicsSystem))]

    still experiencing this issue without any lead to what may cause it.
     
  3. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    286
    In entities 0.51 you might need this, or the write version. I think you need this in all systems that interact with the physics. If you are still on 0.17, I don't know whats going wrong.

    protected override void OnStartRunning()
    {
    this.RegisterPhysicsRuntimeSystemReadOnly();
    }