Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to check if it has a specific component in parallel job & [BurstCompile]

Discussion in 'Entity Component System' started by KANIYO, May 29, 2023.

  1. KANIYO

    KANIYO

    Joined:
    Nov 12, 2020
    Posts:
    23
    I want to check if the Entity returned by `RayCast()` has `GroundTag` or not.
    I know I can check with `HasComponent<GroundTag>()` if it has an EntityManager, but I get an error when I pass state.EntityManager to IjobEntity.
    I want to run this job with `ScheduleParallel()` and `[BurstCompile]`.

    Unity.Entities : 1.0.10
    Any help would be greatly appreciated.

    Code (CSharp):
    1.  
    2. [BurstCompile]
    3. [UpdateInGroup(typeof(PhysicsSystemGroup))]
    4. [UpdateAfter(typeof(PhysicsSimulationGroup))]
    5. public partial struct CheckGroundedSystem : ISystem
    6. {
    7.  
    8.   [BurstCompile]
    9.   public void OnUpdate(ref SystemState state)
    10.   {
    11.  
    12.     new CheckGroundedJob
    13.     {
    14.       DeltaTime = SystemAPI.Time.DeltaTime,
    15.       CollisionWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld,
    16.     }.ScheduleParallel();
    17.   }
    18. }
    19.  
    20.  
    21. [BurstCompile]
    22. public partial struct CheckGroundedJob : IJobEntity
    23. {
    24.   public float DeltaTime;
    25.   [ReadOnly] public CollisionWorld CollisionWorld;
    26.  
    27.   [BurstCompile]
    28.   private void Execute(RefRO<LocalTransform> localTransform, RefRO<GroundingData> grounding, [EntityIndexInQuery] int sortKey)
    29.   {
    30.     float3 RayFrom = localTransform.ValueRO.Position - new float3(0, 1.1f, 0);
    31.     float3 RayTo = localTransform.ValueRO.Position - new float3(0, 1.5f, 0);
    32.     Entity entity = Raycast(RayFrom, RayTo, CollisionWorld);
    33.     if (entity != Entity.Null)
    34.     {
    35.  
    36.        //
    37.        //  I want to check if the entity returned by
    38.        //   `RayCast()` has `GroundTag` or not here!.
    39.        //
    40.  
    41.     }
    42.   }
    43.  
    44.   [BurstCompile]
    45.   Entity Raycast(float3 RayFrom, float3 RayTo, CollisionWorld CollisionWorld)
    46.   {
    47.     RaycastInput input = new RaycastInput()
    48.     {
    49.       Start = RayFrom,
    50.       End = RayTo,
    51.       Filter = new CollisionFilter()
    52.       {
    53.         BelongsTo = ~0u,
    54.         CollidesWith = ~0u, // all 1s, so all layers, collide with everything
    55.         GroupIndex = 0
    56.       }
    57.     };
    58.  
    59.     RaycastHit hit = new RaycastHit();
    60.     bool haveHit = CollisionWorld.CastRay(input, out hit);
    61.     if (haveHit)
    62.     {
    63.       return hit.Entity;
    64.     }
    65.     else
    66.     {
    67.       return Entity.Null;
    68.     }
    69.   }
    70. }
     
    Last edited: May 29, 2023
  2. KANIYO

    KANIYO

    Joined:
    Nov 12, 2020
    Posts:
    23
    Self resolved.

    // in IJobEntity
    [ReadOnly] public ComponentLookup<GroundTag> GroundTagLookup;

    // in ISyetem
    GroundTagLookup = SystemAPI.GetComponentLookup<GroundTag>(true),

    // execute() in IJobEntity.
    GroundTagLookup.HasComponent(entity)


    Thanks
     
  3. KANIYO

    KANIYO

    Joined:
    Nov 12, 2020
    Posts:
    23
    Also, this content was ECS, not Physics.
    My apologies.
     
  4. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    @KANIYO : No worries. I moved your thread to the appropriate forum section so that others can benefit from the solution you found.
     
    KANIYO likes this.
  5. KANIYO

    KANIYO

    Joined:
    Nov 12, 2020
    Posts:
    23
    Thanks:)
     
    daniel-holz likes this.