Search Unity

Resolved What is the proper way to perform a multithreaded Capsule Collider using DOTS?

Discussion in 'Physics for ECS' started by Serenian, Mar 8, 2021.

  1. Serenian

    Serenian

    Joined:
    Jan 18, 2018
    Posts:
    4
    My question is the following:
    • What is the proper way to perform a multithreaded Capsule Collider cast using DOTS?
    My objective is the following:
    • Perform a series of collision casts before moving the player character to determine valid move locations.
    Details:

    I am trying to cast a capsule collider within a Entities.ForEach() loop.

    The error I get is usually some variation of the following message:

    EntityBodyIndexMap is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.

    The problem is solved if I invoke the method call Schedule() rather than ScheduleParallel() at the end of the ForEach loop, while isolating the CollisionWorld and PhysicsWorld references outside the ForEach loop.
     
    kiaat likes this.
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Serenian likes this.
  3. Serenian

    Serenian

    Joined:
    Jan 18, 2018
    Posts:
    4
    SOLVED SOLUTION
    Thank you! This did the trick!

    For future folks interested in the solution, here it is:

    Code (CSharp):
    1. World world = World.DefaultGameObjectInjectionWorld;
    2. BuildPhysicsWorld buildPhysicsWorld = world.GetExistingSystem<BuildPhysicsWorld>();
    3. PhysicsWorld physicsWorld = buildPhysicsWorld.PhysicsWorld;
    4. CollisionWorld collisionWorld = physicsWorld.CollisionWorld;
    5.          
    6. Entities.WithReadOnly(collisionWorld).ForEach((Entity e,  ref Translation translation, ref Rotation rotation) =>
    7. {
    8.     // Your code here for casting colliders
    9.     ColliderCastHit hit = new ColliderCastHit();
    10.     bool haveHit = collisionWorld.CastCollider(c, out hit);
    11. });
     
    joshhart, kiaat, Rowlan and 3 others like this.