How to pass PhysicsWorld to CollisionEventJob to get collisionDetails?

Right now I’m trying to do this:

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

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(ExportPhysicsWorld))]
[UpdateBefore(typeof(EndFramePhysicsSystem))]
public partial class SphereCollisionSystem : SystemBase
{
    BuildPhysicsWorld buildPhysicsWorldSystem;
    StepPhysicsWorld stepPhysicsWorldSystem;

    protected override void OnCreate()
    {
        buildPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<BuildPhysicsWorld>();
        stepPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<StepPhysicsWorld>();
    }

    [BurstCompile]
    private struct CollisionEventJob : ICollisionEventsJob
    {
        public PhysicsWorld physicsWorld;
        public void Execute(CollisionEvent collisionEvent)
        {
            Debug.Log("Collision");
            CollisionEvent.Details collisionDetails = collisionEvent.CalculateDetails(ref physicsWorld);
            Debug.Log(string.Format("{0}", collisionDetails));
        }
    }

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

    protected override void OnUpdate()
    {
        JobHandle collisionJobHandle = new CollisionEventJob()
        {
            physicsWorld = buildPhysicsWorldSystem.PhysicsWorld
        }.Schedule(stepPhysicsWorldSystem.Simulation, this.Dependency);
        collisionJobHandle.Complete();
    }
}

But in response I get an error:

InvalidOperationException: The previously scheduled job PhysicsWorldExporter:ExportDynamicBodiesJob reads from the Unity.Collections.NativeArray`1[Unity.Physics.MotionData] ExportDynamicBodiesJob.JobData.MotionDatas. You are trying to schedule a new job SphereCollisionSystem:CollisionEventJob, which writes to the same Unity.Collections.NativeArray`1[Unity.Physics.MotionData] (via CollisionEventJob.UserJobData.physicsWorld.DynamicsWorld.m_MotionDatas). To guarantee safety, you must include PhysicsWorldExporter:ExportDynamicBodiesJob as a dependency of the newly scheduled job.
Unity.Jobs.LowLevel.Unsafe.JobsUtility.Schedule (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters) (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
Unity.Physics.ICollisionEventJobExtensions.ScheduleUnityPhysicsCollisionEventsJob[T] (T jobData, Unity.Physics.ISimulation simulation, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.51.1-preview.21/Unity.Physics/Dynamics/Simulation/ICollisionEventsJob.cs:83)
Unity.Physics.ICollisionEventJobExtensions.Schedule[T] (T jobData, Unity.Physics.ISimulation simulation, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.51.1-preview.21/Unity.Physics/Dynamics/Simulation/ICollisionEventsJob.cs:45)
SphereCollisionSystem.OnUpdate () (at Assets/Scripts/Systems/SphereCollisionSystem.cs:41)
Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/SystemBase.cs:409)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:583)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:588)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:529)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:583)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:523)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ScriptBehaviourUpdateOrder.cs:426)

I’m pretty sure you need to add [ReadOnly] to the physicsWorld field of your job

1 Like

Oh yes, it helped, thanks)