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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Ground check raycast system doesn't hit anything

Discussion in 'Physics for ECS' started by AndrelMed, May 21, 2020.

  1. AndrelMed

    AndrelMed

    Joined:
    May 5, 2018
    Posts:
    2
    Hi everyone!
    After having searched and learned a lot through these threads, i still can't realize what i'm doing wrong in this ground check system...
    I can't get my ray hits something.
    Here it is the code, very simple:
    Code (CSharp):
    1. [UpdateAfter(typeof(ExportPhysicsWorld)), UpdateBefore(typeof(EndFramePhysicsSystem))]
    2. public class GroundCheckSystem : SystemBase
    3. {
    4.  
    5.     BuildPhysicsWorld _BuildPhysicsWorldSystem;
    6.  
    7.     protected override void OnCreate() {
    8.         _BuildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
    9.     }
    10.  
    11.  
    12.     protected override void OnUpdate( )
    13.     {
    14.  
    15.         Dependency = JobHandle.CombineDependencies(_BuildPhysicsWorldSystem.FinalJobHandle, Dependency);
    16.  
    17.         var collisionWorld = _BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
    18.  
    19.         Entities
    20.         .WithReadOnly(collisionWorld)
    21.         .ForEach((ref CharacterDataComponent data, in Translation position, in PhysicsCollider collider ) =>
    22.         {
    23.             RaycastInput input = new RaycastInput()
    24.             {
    25.                 Start = position.Value,
    26.                 End = position.Value - new float3(0,1.5f,0),
    27.                 Filter = collider.Value.Value.Filter
    28.             };
    29.  
    30.             data.isGrounded = collisionWorld.CastRay(input);
    31.  
    32.         }).ScheduleParallel();
    33.      
    34.     }
    35.  
    36. }

    Any help is really appreciated!
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    This all seems fine. I'd check a couple of things (sorry if some of them are too silly):
    - Check the filter on your collider; you can also try to cast a ray with the default filter to see if anything hits... Also, is your world setup in a way that filters should actually allow some hits?
    - Is Y axis your UP axis? Also, is there anything 1.5m or closer to the character along the Y axis? Maybe try to increase it to say 10m and verify.

    This is a side note, but did you try to use the CharacterControllerUtilities.CheckSupport()? It's meant to do exactly what you need.
     
  3. AndrelMed

    AndrelMed

    Joined:
    May 5, 2018
    Posts:
    2
    Hi, thanks you very much for your answer!
    I just got it working, but honestly I don't really know what has done the trick :cool:
    My collision filters are fine I think.. i've set the ground belongs to 0 and player belongs to 1. and both collide with everythings except for their respective layer.
    In the code i've added some debug draws and edited the foreach loop by adding
    Dependency =
    Entity...................ScheduleParallel(Dependency).
    Then i've also rewritten the RaycastInput in another way using LocalToWorld component instead of Translation. maybe this made it works.
    I'm not sure, but now it works and here it is the new code:

    Code (CSharp):
    1. [UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore((typeof(EndFramePhysicsSystem)))]
    2. public class GroundCheckSystem : 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.         Dependency = JobHandle.CombineDependencies(Dependency, m_BuildPhysicsWorldSystem.FinalJobHandle);
    14.    
    15.         CollisionWorld world = m_BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
    16.  
    17.         Dependency = Entities
    18.         .WithReadOnly(world)
    19.         .ForEach((ref CharacterDataComponent data, in LocalToWorld t, in PhysicsCollider pc) =>
    20.         {  
    21.  
    22.             RaycastInput RayInput = new RaycastInput
    23.             {
    24.                 Start = t.Position + t.Up * data.verticalOffset,
    25.                 End = (t.Position + t.Up * data.verticalOffset) - t.Up * data.rayDistance,
    26.                 Filter = pc.Value.Value.Filter
    27.             };
    28.  
    29.             data.isGrounded = world.CastRay(RayInput, out Unity.Physics.RaycastHit hit);
    30.  
    31.             //Debug
    32.             if(data.drawDebugGizmos) {
    33.  
    34.                 Debug.DrawRay(RayInput.Start,-t.Up * data.rayDistance, Color.green);
    35.  
    36.                 if(data.isGrounded) {
    37.                    
    38.                     Debug.DrawLine(hit.Position-new float3(.2f,.2f,0), hit.Position+new float3(.2f,.2f,0),Color.red,.05f);
    39.                     Debug.DrawLine(hit.Position-new float3(.2f,-.2f,0), hit.Position+new float3(.2f,-.2f,0),Color.red,.05f);
    40.  
    41.                 }
    42.             }
    43.  
    44.         }).ScheduleParallel(Dependency);
    45.    
    46.     }
    47.  
    48. }
     
    Last edited: May 22, 2020
    petarmHavok likes this.