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?