EDIT: Please see #4 for a clean, no-dependency repro example.
Original post below:
Could I get some help, either pointing me to where a good “example” of doing system entity job raycasts would be, or helping me troubleshoot the following? Also if there are some good physics examples out there, I’d
The following code “worked” in the 0.5 version. in 0.6. It’s a simple raycast in an Entities.ForEach()
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Jobs;
[UpdateAfter(typeof(EndFramePhysicsSystem))]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public class EnemySystem : SystemBase
{
protected override void OnCreate()
{
base.OnCreate();
}
protected override void OnUpdate()
{
var bpw = World.GetOrCreateSystem<BuildPhysicsWorld>();
var pw = bpw.PhysicsWorld;
Entities
.WithReadOnly(pw)
.ForEach((int nativeThreadIndex, ref Movable mov, ref Enemy enemy, in Translation trans) =>
{
var targetDir = new float3(0, 0, -1);
var ray = new RaycastInput()
{
Start = trans.Value,
End = trans.Value + (targetDir * 0.9f),
Filter = new CollisionFilter()
{
GroupIndex = 0,
BelongsTo = 1u << 1, //bitmasks, so using bitshifts
CollidesWith = 1u << 2
}
};
var hit = pw.CastRay(ray, out Unity.Physics.RaycastHit closestHit);
if (hit)
{
mov.direction = targetDir;
}
enemy.lastPos = trans.Value;
})
.ScheduleParallel();
}
}
I’m getting a runtime error saying Exception Trace
InvalidOperationException: The previously scheduled job Broadphase:prepareStaticBodyDataJob writes to the Unity.Collections.NativeArray1[Unity.Physics.CollisionFilter] PrepareStaticBodyDataJob.FiltersOut. You are trying to schedule a new job EnemySystem:OnUpdate_LambdaJob0, which reads from the same Unity.Collections.NativeArray
1[Unity.Physics.CollisionFilter] (via OnUpdate_LambdaJob0.JobData.pw.CollisionWorld.Broadphase.m_StaticTree.BodyFilters). To guarantee safety, you must include Broadphase:prepareStaticBodyDataJob as a dependency of the newly scheduled job.
Unity.Entities.JobChunkExtensions.FinalizeScheduleChecked (System.Boolean isParallel, System.Int32 unfilteredChunkCount, Unity.Jobs.JobHandle prefilterHandle, Unity.Collections.NativeArray`1[T] prefilterData, System.Void* deferredCountData, System.Boolean useFiltering, Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& scheduleParams, System.Boolean& executed, Unity.Jobs.JobHandle& result) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/IJobChunk.cs:270)
Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode, System.Boolean isParallel) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/IJobChunk.cs:246)
Unity.Entities.JobChunkExtensions.ScheduleParallel[T] (T jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/IJobChunk.cs:150)
EnemySystem.OnUpdate () (at Assets/Scripts/Systems/EnemySystem.cs:361)
Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/SystemBase.cs:412)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:472)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:477)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:423)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ScriptBehaviourUpdateOrder.cs:333)
That error basically says I need to call Job.Complete() because my job is still running.
If I call this.Dependency.Complete() it works again. But… I was told in a dots thread that I should try to use the AddInputDependency() and GetOutputDependency() from the PhysicsWorld.
So I tried a bunch of threads and can’t figure it out. Also there doesn’t seem to be any documentation on StepPhysicsWorld, EndFramePhysicsSystem, etc… anywhere. Like what those systems do and what order they operate. I get the feeling that EndFramePhysicsSystem goes last and StepPhysicsWorld is first, but when I tried setting m_StepPhysicsWorld.AddInputDependency(myRayCastJobHandle) I get a bunch of other errors because other systems are accessing physics data at the same time as my job.
If there’s some details on these systems I would really love to see.