Raycasts not hitting pure entities

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:

private void CreateStarEntity()
    {
        NativeArray<Entity> entityArray = new NativeArray<Entity>(numSystems, Allocator.Temp);
        entityManager.CreateEntity(starArchetype, entityArray);

        for (int i = 0; i < entityArray.Length; i++)
        {
            //SetSeed();
            UnityEngine.Material mat = SetStarMaterial();
            Entity entity = entityArray[i];
            if (spiralGalaxy)
            {
                int armChance = UnityEngine.Random.Range(0, 100);
                if(armChance > spiralConcentration)
                {
                    entityManager.SetComponentData(entity, new Translation
                    {
                        Value = GetRandomFloat3()
                    });
                }
                else
                {
                    int arm = UnityEngine.Random.Range(0, spiralArms);

                    entityManager.SetComponentData(entity, new Translation
                    {
                        Value = GetRandomSpiralFloat3(arm)
                    });
                }
               
            }
            else
            {
                entityManager.SetComponentData(entity, new Translation
                {
                    Value = GetRandomFloat3()
                });
            }
           
           
            entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = starMesh, material = mat });
            entityManager.SetComponentData(entity, new Scale { Value = SetScale(mat)});
            entityManager.SetName(entity, "Bob");
        }
        entityArray.Dispose();
    }

    private void CreateStarArchetype()
    {
        entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
        starArchetype = entityManager.CreateArchetype(
            typeof(CelestialObjectComponent),
            typeof(StarComponent),
            typeof(Translation),
            typeof(PhysicsCollider),
            typeof(Scale),
            typeof(Rotation),
            typeof(RenderMesh),
            typeof(RenderBounds),
            typeof(LocalToWorld)
            );
    }

And here is the RayCast:

private Entity Raycast()
    {
        float raydistance = 100000f;
        UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float3 fromPosition = ray.origin;
        float3 toPosition = ray.direction * raydistance;
        BuildPhysicsWorld buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
        CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
        RaycastInput raycastInput = new RaycastInput
        {
            Start = fromPosition,
            End = toPosition,
            Filter = new CollisionFilter
            {
                BelongsTo = ~0u,
                CollidesWith = ~0u,
                GroupIndex = 0,
            }
        };
        Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
        if(collisionWorld.CastRay(raycastInput, out raycastHit))
        {
            Entity hitEntity = buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
            return hitEntity;
        }
        else
        {
            return Entity.Null;
        }
    }

What am I missing?

1 Like

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.

2 Likes

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!

1 Like

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:

entityManager.SetComponentData(entity, new PhysicsCollider { Value = Unity.Physics.SphereCollider.Create(sphere, CollisionFilter.Default) });
2 Likes