Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How to properly schedule an IContactsJob to access components later?

Discussion in 'Physics for ECS' started by nickedname, Jul 13, 2022.

  1. nickedname


    Oct 5, 2013

    I tried writing some code to create one-way platforms based on the unity physics samples in Demos/Modify/ModifyContactJacobians.unity.

    Here's the result I am trying to achieve (I can walk through when going right, but I walk on top of the platform when I go left once I am no longer colliding).

    For the code I request the LocalWorld.Up variable by passing GetComponentData<LocalToWorld>(true) among others to an IContactJob and queueing the IContactJob in the PostCreateContacts callback
    Code (CSharp):
    1. stepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostCreateContacts, beforePhysicsCallback, Dependency);
    The code works fine until I request LocalToWorld by ref anywhere else in my code by e.g. by calling
    Code (CSharp):
    1. Dependency = Entities.ForEach((ref LocalToWorld world)=>{
    2. }).Schedule(Dependency);
    I receive the following errors regarding Dependency:
    "The system Unity.Physics.Systems.StepPhysicsWorld reads Player via PlatformSystem:SetContactFlagsJob but that type was not assigned to the Dependency property."

    Currently, I am clueless on how to provide proper dependency resolution in the void EnqueueCallback function as the Dependency = x.Schedule(Dependency) method of dependency resolution doesn't apply here.

    Is there is a different way to retrieve dependencies from the EnqueueCallback function?
    Should I make the physics system readonly using RegisterPhysicsRuntimeSystemReadOnly()?

    As the simulation_modification docs stated "In here, you should schedule any jobs you need and return the combined JobHandles of any jobs you schedule – this will prevent future physics tasks from processing data before you are done with it."

    Does this imply that variables passed to any IContactsJob can only be used inside physics tasks?

    I am also open for suggestions on how to implement platform entities in a different way in ECS as I approached the ModifyContactJacobian sample method simply because the sample showcased how to disable colliders.

    Hopefully someone can help resolve my confusion and hint me in the right direction.

    Inside the Unity physics sample I tested that the ModifyContacJacobians sample has similar errors by adding an Entities.ForEach to OnUpdate.

    Since the code isn't that important to me, I added a gist with some of my code and a modified ModifyContactJacobiansBehaviour.cs here for reference: