Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Raycasts not hitting pure entities

Discussion in 'DOTS Physics' started by Twanner, Aug 9, 2020.

  1. Twanner

    Twanner

    Joined:
    Dec 6, 2014
    Posts:
    29
    Hey everyone. I'm running into an issue with raycasts on pure entities. The raycasts work fine on converted game objects but for entities that I generate purely in code I cannot get a hit.

    Here is the code for creating the entity:

    Code (CSharp):
    1. private void CreateStarEntity()
    2.     {
    3.         NativeArray<Entity> entityArray = new NativeArray<Entity>(numSystems, Allocator.Temp);
    4.         entityManager.CreateEntity(starArchetype, entityArray);
    5.  
    6.         for (int i = 0; i < entityArray.Length; i++)
    7.         {
    8.             //SetSeed();
    9.             UnityEngine.Material mat = SetStarMaterial();
    10.             Entity entity = entityArray[i];
    11.             if (spiralGalaxy)
    12.             {
    13.                 int armChance = UnityEngine.Random.Range(0, 100);
    14.                 if(armChance > spiralConcentration)
    15.                 {
    16.                     entityManager.SetComponentData(entity, new Translation
    17.                     {
    18.                         Value = GetRandomFloat3()
    19.                     });
    20.                 }
    21.                 else
    22.                 {
    23.                     int arm = UnityEngine.Random.Range(0, spiralArms);
    24.  
    25.                     entityManager.SetComponentData(entity, new Translation
    26.                     {
    27.                         Value = GetRandomSpiralFloat3(arm)
    28.                     });
    29.                 }
    30.                
    31.             }
    32.             else
    33.             {
    34.                 entityManager.SetComponentData(entity, new Translation
    35.                 {
    36.                     Value = GetRandomFloat3()
    37.                 });
    38.             }
    39.            
    40.            
    41.             entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = starMesh, material = mat });
    42.             entityManager.SetComponentData(entity, new Scale { Value = SetScale(mat)});
    43.             entityManager.SetName(entity, "Bob");
    44.         }
    45.         entityArray.Dispose();
    46.     }
    47.  
    48.     private void CreateStarArchetype()
    49.     {
    50.         entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    51.         starArchetype = entityManager.CreateArchetype(
    52.             typeof(CelestialObjectComponent),
    53.             typeof(StarComponent),
    54.             typeof(Translation),
    55.             typeof(PhysicsCollider),
    56.             typeof(Scale),
    57.             typeof(Rotation),
    58.             typeof(RenderMesh),
    59.             typeof(RenderBounds),
    60.             typeof(LocalToWorld)
    61.             );
    62.     }
    And here is the RayCast:

    Code (CSharp):
    1. private Entity Raycast()
    2.     {
    3.         float raydistance = 100000f;
    4.         UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.         float3 fromPosition = ray.origin;
    6.         float3 toPosition = ray.direction * raydistance;
    7.         BuildPhysicsWorld buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
    8.         CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
    9.         RaycastInput raycastInput = new RaycastInput
    10.         {
    11.             Start = fromPosition,
    12.             End = toPosition,
    13.             Filter = new CollisionFilter
    14.             {
    15.                 BelongsTo = ~0u,
    16.                 CollidesWith = ~0u,
    17.                 GroupIndex = 0,
    18.             }
    19.         };
    20.         Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    21.         if(collisionWorld.CastRay(raycastInput, out raycastHit))
    22.         {
    23.             Entity hitEntity = buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
    24.             return hitEntity;
    25.         }
    26.         else
    27.         {
    28.             return Entity.Null;
    29.         }
    30.     }

    What am I missing?
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    291
    First thing that comes to my mind, your entities seem to be missing PhysicsCollider on them? You've provided that in the archetype, but haven't set a value for it, so the collider is not there and even if it defaults to something, it doesn't mean it's represented the same as your render object, and thus the missed ray cast.
     
    Twanner likes this.
  3. Twanner

    Twanner

    Joined:
    Dec 6, 2014
    Posts:
    29

    Thanks! I see what you mean, I'll give it a try, although I'm not exactly sure how. I'll do some research on the api but if you know how that should be written I'd love a tip!
     
  4. Twanner

    Twanner

    Joined:
    Dec 6, 2014
    Posts:
    29
    Update:

    Your suggestion worked, Thanks a ton! So I had to set the PhysicsCollider Value to a collider geometry and collision filter.

    Here is the functional code line that I was missing:
    Code (CSharp):
    1. entityManager.SetComponentData(entity, new PhysicsCollider { Value = Unity.Physics.SphereCollider.Create(sphere, CollisionFilter.Default) });
     
    petarmHavok likes this.
unityunity