Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

restricted IJobParallelFor exception during raycast call

Discussion in 'Physics for ECS' started by BigRookGames, Dec 13, 2020.

  1. BigRookGames

    BigRookGames

    Joined:
    Nov 24, 2014
    Posts:
    330
    I am receiving a "
    IndexOutOfRangeException: Index 1 is out of restricted IJobParallelFor range [0...0] in ReadWriteBuffer.
    ReadWriteBuffers are restricted to only read & write the element at the job index. You can use double buffering strategies to avoid race conditions due to reading & writing in parallel to the same elements from a job.
    " exception within this:

    Code (CSharp):
    1. JobHandle hoverJob = Entities
    2.             .WithBurst()
    3.             .ForEach((Entity entity
    4.             , int entityInQueryIndex //Used for Concurrent ECB writing
    5.             , ref HoverSystemComponent hoverSystem
    6.             , ref PhysicsVelocity _physicsVelocity
    7.             , ref Translation translation
    8.             , in PhysicsMass _physicsMass
    9.             ) =>
    10.         {
    11.             RaycastHit raycastHit = new RaycastHit();
    12.             RaycastInput rayInput = new RaycastInput
    13.             {
    14.                 Start = translation.Value,
    15.                 End = translation.Value - new float3(0, hoverSystem.hoverHeight, 0),
    16.                 Filter = colFilter2
    17.             };
    18.             bool hit = collisionWorld.CastRay(rayInput, out raycastHit);
    19.  
    20.             if (hit)
    21.             {
    22.                 ...
    23.             }
    24.         }).ScheduleParallel(startAfter);
    and it appears to be triggered in Library/PackageCache/com.unity.physics@0.5.1-preview.2/Unity.Physics/Collision/World/Broadphase.cs:590)
    which is:
    RigidBody body = m_Bodies[rigidBodyIndex];

    Is there any way around this to get raycast calls to work inside of parallelfor jobs ?
     
  2. BigRookGames

    BigRookGames

    Joined:
    Nov 24, 2014
    Posts:
    330
    Nevermind. It is fixed when using a read only reference of the collisionWorld.
     
    petarmHavok likes this.
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Was about to say that. :)