Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Resolved [Help Needed] Can't get RayCast to work in SystemBase system

Discussion in 'DOTS Physics' started by RBogdy, Apr 17, 2020.

  1. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    Hello,

    I have a system for collecting player input from mouse, perform a raycast on a plane and store the hit position in a component for later use, however I can't get it to work

    Code (CSharp):
    1. [UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore((typeof(EndFramePhysicsSystem)))]
    2. public class InputToWorldSpaceSystem : SystemBase
    3. {
    4.     BuildPhysicsWorld m_BuildPhysicsWorldSystem;
    5.  
    6.     protected override void OnCreate()
    7.     {
    8.         m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    9.     }
    10.  
    11.     protected override void OnUpdate()
    12.     {
    13.         Mouse mouse = Mouse.current;
    14.         Dependency = JobHandle.CombineDependencies(Dependency, m_BuildPhysicsWorldSystem.FinalJobHandle);
    15.        
    16.         if (mouse.leftButton.isPressed)
    17.         {
    18.             CollisionWorld CollisionWorld = m_BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
    19.             UnityEngine.Ray ray = Camera.main.ScreenPointToRay(mouse.position.ReadValue());          
    20.             float farClipPlane = Camera.main.farClipPlane;
    21.  
    22.             RaycastInput RayInput = new RaycastInput
    23.             {
    24.                 Start = ray.origin,
    25.                 End = ray.origin + ray.direction * farClipPlane,
    26.                 Filter = new CollisionFilter()
    27.                 {
    28.                     BelongsTo = 1u << 0,
    29.                     CollidesWith = 1u << 2
    30.                 }
    31.             };
    32.  
    33.             Dependency = Entities
    34.                 .WithReadOnly(CollisionWorld)
    35.                 .ForEach((ref PlayerInput input) =>
    36.                 {                  
    37.                     bool haveHit = CollisionWorld.CastRay(RayInput, out Unity.Physics.RaycastHit hit);
    38.  
    39.                     if(haveHit)
    40.                     {
    41.                         input.WorldPosition = hit.Position;
    42.                     }
    43.  
    44.                 }).Schedule(Dependency);
    45.         }
    46.     }
    47. }

    InvalidOperationException: The previously scheduled job InputToWorldSpaceSystem:<>c__DisplayClass_OnUpdate_LambdaJob0 reads from the Unity.Collections.NativeArray`1[Unity.Physics.CollisionFilter] <>c__DisplayClass_OnUpdate_LambdaJob0.JobData.CollisionWorld.Broadphase.m_StaticTree.BodyFilters. You are trying to schedule a new job Broadphase:prepareStaticBodyDataJob, which writes to the same Unity.Collections.NativeArray`1[Unity.Physics.CollisionFilter] (via PrepareStaticBodyDataJob.FiltersOut). To guarantee safety, you must include I
    Unity.Jobs.LowLevel.Unsafe.JobsUtility.ScheduleParallelForDeferArraySize (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters, System.Int32 innerloopBatchCount, System.Void* listData, System.Void* listDataAtomicSafetyHandle) (at <f56bb44ba853493d87ccc25c2e339f8a>:0)
    Unity.Jobs.IJobParallelForDeferExtensions.Schedule[T] (T jobData, System.Int32* forEachCount, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.jobs@0.2.8-preview.3/Unity.Jobs/IJobParallelForDefer.cs:111)
    Unity.Jobs.IJobParallelForDeferExtensionsPhysics.ScheduleUnsafeIndex0[T] (T jobData, Unity.Collections.NativeArray`1[T] forEachCount, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Base/Jobs/IJobParallelForDeferExtensionsPhysics.cs:11)
    Unity.Physics.Broadphase.ScheduleStaticTreeBuildJobs (Unity.Physics.PhysicsWorld& world, System.Int32 numThreadsHint, Unity.Collections.NativeArray`1[T] shouldDoWork, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/World/Broadphase.cs:186)
    Unity.Physics.Broadphase.ScheduleBuildJobs (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Mathematics.float3 gravity, Unity.Collections.NativeArray`1[T] buildStaticTree, Unity.Jobs.JobHandle inputDeps, System.Int32 threadCountHint) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/World/Broadphase.cs:157)
    Unity.Physics.CollisionWorld.ScheduleBuildBroadphaseJobs (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Mathematics.float3 gravity, Unity.Collections.NativeArray`1[T] buildStaticTree, Unity.Jobs.JobHandle inputDeps, System.Int32 threadCountHint) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/World/CollisionWorld.cs:82)
    Unity.Physics.Systems.BuildPhysicsWorld.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/ECS/Systems/BuildPhysicsWorld.cs:241)
    Unity.Entities.JobComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/JobComponentSystem.cs:129)
    Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/ComponentSystemGroup.cs:134)
    UnityEngine.Debug:LogException(Exception)
    Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Stubs/Unity/Debug.cs:19)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/ComponentSystemGroup.cs:138)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/ComponentSystemGroup.cs:114)
    Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/ComponentSystem.cs:108)
    Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/ScriptBehaviourUpdateOrder.cs:200)

    Using Entities 0.9.1 and Unity Physics 0.3.2
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    Your job overlaps with the next step's bounding volume hierarchy building. Please add the dependency to the EndFramePhysicsSystem.HandlesToWaitFor to make sure it finishes before that system is done.
     
    lclemens and florianhanke like this.
  3. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    Thank you, that solved it
     
    petarmHavok likes this.
  4. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    268
    Dependency management is confusing... :)
    How would one sandwich a system like this between BuildPhysicsWorld and its subsequent systems?
     
  5. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    With the latest (0.4.0 release) you have AddInputDependency() and GetOutputDependency() on all core physics systems - BuildPhysicsWorld, StepPhysicsWorld, ExportPhysicsWorld and EndFramePhysicsSystem. So you can now squeeze in a job in between any of the two, and even before the first one.
     
    Nothke likes this.
unityunity