Slice issue with ECS raycasting

Trying out raycasting with ECS, Entites 0.1.1, Unity 2019.3.0b7. Probably a silly mistake on my part,

The code works when not done within a job (ComponentSystem w/ Entities.ForEach), but when in a job I get a sea of this stack trace:

ArgumentException: Slice start + length (4) range must be <= array.Length (0)
Unity.Collections.NativeSlice`1[T]..ctor (Unity.Collections.NativeArray`1[T] array, System.Int32 start, System.Int32 length) (at <5e35e4589c1948aa8af5b8e64eea8798>:0)
Unity.Physics.CollisionWorld.get_Bodies () (at Library/PackageCache/com.unity.physics@0.2.4-preview/Unity.Physics/Collision/World/CollisionWorld.cs:19)
Unity.Physics.PhysicsWorld.get_StaticBodies () (at Library/PackageCache/com.unity.physics@0.2.4-preview/Unity.Physics/Dynamics/World/PhysicsWorld.cs:20)
Unity.Physics.Systems.BuildPhysicsWorld.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.2.4-preview/Unity.Physics/ECS/Systems/BuildPhysicsWorld.cs:152)
Unity.Entities.JobComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:933)
Unity.Entities.ComponentSystemBase.Update () (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ComponentSystem.cs:284)
Halloween.Prototyping.FixedUpdate () (at Assets/Scripts/Level/Prototyping.cs:41)

The source:

public class PlayerGroundedSystem : JobComponentSystem
    {
        struct PlayerGroundedJob : IJobForEach<PlayerGroundedData>
        {
            public CollisionWorld CWorld;
            public void Execute(ref PlayerGroundedData data)
            {
                RaycastInput rci = new RaycastInput
                {
                    Start = new float3(30, 0, 30),
                    End = new float3(60, 0, 60),
                    Filter = CollisionFilter.Default,
                };

                data.Grounded = CWorld.CastRay(rci);
            }
        }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            BuildPhysicsWorld pWorld = World.Active.GetExistingSystem<BuildPhysicsWorld>();
            CollisionWorld collisionWorld = pWorld.PhysicsWorld.CollisionWorld;
            PlayerGroundedJob job = new PlayerGroundedJob
            {
                CWorld = collisionWorld,
            };
            return job.Schedule(this, inputDeps);
        }
    }

I took this from Spatial queries and filtering | Package Manager UI website, for reference.

1 Like

Scratch that, solved ( :

            JobHandle combined = JobHandle.CombineDependencies(inputDeps, buildPhysicsWorld.FinalJobHandle);
            JobHandle handle = job.Schedule(this, combined);
1 Like