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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Cast ray against a single collider without using CollisionWorld

Discussion in 'Physics for ECS' started by GameDeveloper1111, Apr 19, 2022.

  1. GameDeveloper1111

    GameDeveloper1111

    Joined:
    Jul 24, 2020
    Posts:
    100
    Solved

    tertle's advice worked:
    ---

    I'm trying to cast a ray against a particular collider without using CollisionWorld.

    I tried the following and I'm not getting a hit: `physicsCollider.Value.Value.CastRay(raycastInput)`

    Using `collisionWorld.CastRay(raycastInput)` works but seems to be doing extra work, even if I use a CollisionFilter that filters out everything except the collider of interest.

    Interestingly, `physicsCollider.Value.Value.CastCollider(colliderCastInput)` *does* work.

    Here is the code I'm using, in the `1a. Hello World` PhysicsSamples scene, Unity 2020.3.30f1, Entities 0.50:

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Physics;
    4. using Unity.Mathematics;
    5. using Unity.Transforms;
    6. using Debug = UnityEngine.Debug;
    7.  
    8. public partial class TestSystem : SystemBase
    9. {
    10.     protected override void OnUpdate()
    11.     {
    12.         Entities
    13.             .WithAll<TestTag>()
    14.             .ForEach((Entity entity, in Translation translation, in PhysicsCollider physicsCollider) => {
    15.                 var raycastInput = new RaycastInput
    16.                 {
    17.                     Start  = translation.Value - 10f * math.forward(),
    18.                     End    = translation.Value + 10f * math.forward(),
    19.                     Filter = CollisionFilter.Default
    20.                 };
    21.                 if (physicsCollider.Value.Value.CastRay(raycastInput))
    22.                 {
    23.                     Debug.Log($"Hit");
    24.                 }
    25.                 else
    26.                 {
    27.                     Debug.Log($"No hit");  // Always outputs "No hit"
    28.                 }
    29.             }).Schedule();
    30.     }
    31. }

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Physics;
    4. using Unity.Mathematics;
    5. using Unity.Transforms;
    6. using Debug = UnityEngine.Debug;
    7.  
    8. public partial class TestSystem : SystemBase
    9. {
    10.     protected unsafe override void OnUpdate()
    11.     {
    12.         Entities
    13.             .WithAll<TestTag>()
    14.             .ForEach((Entity entity, in Translation translation, in Rotation rotation, in PhysicsCollider physicsCollider) => {
    15.                 var colliderCastInput = new ColliderCastInput
    16.                 {
    17.                     Collider    = physicsCollider.ColliderPtr,
    18.                     Orientation = rotation.Value,
    19.                     Start       = translation.Value - 10f * math.forward(),
    20.                     End         = translation.Value + 10f * math.forward(),
    21.                 };
    22.                 if (physicsCollider.Value.Value.CastCollider(colliderCastInput))
    23.                 {
    24.                     Debug.Log($"Hit");  // Always outputs "Hit".
    25.                 }
    26.                 else
    27.                 {
    28.                     Debug.Log($"No hit");
    29.                 }
    30.             }).Schedule();
    31.     }
    32. }

    I'm also getting this behavior in Unity 2020.3.14f1, Entities 0.17.
     
    Last edited: Apr 19, 2022
    JosepMariaPujol likes this.