Search Unity

Can't get raycasts to work (solved)

Discussion in 'Entity Component System' started by illinar, May 8, 2019.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    I have pure ECS physics world created from converted default boxes. Everything collides as it should but raycasts never hit anything.

    The ray is correct and should collide.

    Lets start with RaycastInput. The CollisionFilter.Default should work for the raycast, right?

    Code (CSharp):
    1.  
    2.             var raycast = new RaycastInput
    3.             {
    4.                 Ray = new Unity.Physics.Ray(new float3(1, 10, 1), new float3(0, -1, 0)),
    5.                 Filter = CollisionFilter.Default,
    6.             };
    7.             Debug.Log("this executes");
    8.             if (RaycastService.Raycast(raycast, ref hit, ref entity))
    9.             {
    10.                 Debug.Log("this never executes");
    11.                 Entities.ForEach((ref PointerCollision c) =>
    12.                 {
    13.                     c.Position = hit.Position;
    14.                 });
    15.             }
    Raycast itself:

    Code (CSharp):
    1.  
    2.     [BurstCompile]
    3.     public struct RaycastJob : IJobParallelFor
    4.     {
    5.         [ReadOnly] public CollisionWorld world;
    6.         [ReadOnly] public NativeArray<RaycastInput> inputs;
    7.         public NativeArray<RaycastHit> results;
    8.  
    9.         public unsafe void Execute(int index)
    10.         {
    11.             RaycastHit hit;
    12.             world.CastRay(inputs[index], out hit);
    13.             results[index] = hit;
    14.         }
    15.     }
    16.  
    17.     public static JobHandle ScheduleBatchRayCast(CollisionWorld world, NativeArray<RaycastInput> inputs, NativeArray<RaycastHit> results)
    18.     {
    19.         JobHandle rcj = new RaycastJob
    20.         {
    21.             inputs = inputs,
    22.             results = results,
    23.             world = world
    24.         }.Schedule(inputs.Length, 5);
    25.         return rcj;
    26.     }
    27.  
    28.     public static bool Raycast(RaycastInput input, ref RaycastHit hit, ref Entity e)
    29.     {
    30.         var world = World.Active.GetExistingSystem<Unity.Physics.Systems.BuildPhysicsWorld>().PhysicsWorld.CollisionWorld;
    31.         var raycasts = new NativeArray<RaycastInput>(1, Allocator.TempJob);
    32.         var hits = new NativeArray<RaycastHit>(1, Allocator.TempJob);
    33.         raycasts[0] = input;
    34.         var handle = ScheduleBatchRayCast(world, raycasts, hits);
    35.         handle.Complete();
    36.         hit = hits[0];
    37.         raycasts.Dispose();
    38.         hits.Dispose();
    39.         if (world.NumBodies > 0 && hit.Fraction > 0 && hit.RigidBodyIndex != 0)
    40.         {
    41.             e = world.Bodies[hit.RigidBodyIndex].Entity;
    42.             return true;
    43.         }
    44.         return false;
    45.     }
    I don't know how to check if raycast had hit anything so instead I just bet on the fact that
    hit.Fraction == 0
    will pretty much only happen when there was no hit.
     
    Last edited: May 8, 2019
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    as @Kichang-Kim says you aren't including any distance so your rays are only travelling 1 unit

    Code (CSharp):
    1. Ray = new Unity.Physics.Ray(new float3(1, 10, 1), new float3(0, -1, 0) * distance)
    If you read the documentation

    Also the documentation sample snippet

    Code (CSharp):
    1.            Ray = new Ray()
    2.            {
    3.                Origin = RayFrom,
    4.                Direction = RayTo - RayFrom
    5.            },
     
  4. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    Thank you so much, guys, that was the issue.
     
  5. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    The ray in the latest package uses the term Displacement instead of Direction, which is less ambiguous. https://docs.unity3d.com/Packages/com.unity.physics@0.6/api/Unity.Physics.Ray.html .