Search Unity

Issues adding dynamically created mesh colliders to entities for physics raycasting

Discussion in 'Entity Component System' started by TheBiter, Jun 24, 2018.

  1. TheBiter

    TheBiter

    Joined:
    May 29, 2013
    Posts:
    14
    So as part of own attempts to better understand how to use Unity's ECS system I am currently trying write a small project that creates a procedural mesh at runtime and have the user be able to click on it with a mouse with an end goal of much more complicated things.

    I was hoping to leverage Unity's mesh colliders and physics raycast system just to not have to rewrite hit detection systems but I am stuck on trying to add a mesh collider at runtime to my entity.

    So I setup my archetype like this:
    Code (CSharp):
    1. var gameMapArchetype = entityManager.CreateArchetype(typeof(GameMap),typeof (MeshCollider));
    2. entityManager.CreateEntity(gameMapArchetype);
    and then in my component system I filter for it like this:

    Code (CSharp):
    1. struct Map
    2. {
    3.    public int Length;
    4.    public EntityArray SpawnedEntities;
    5.    public ComponentDataArray<GameMap> GameMapCreator;
    6. }
    7. [Inject] Map m_gameMap;
    which is then updated in the OnUpdate like this:
    Code (CSharp):
    1. var em = PostUpdateCommands;
    2. Mesh mapMesh = GenerateMapMeshData(tileInfo.Width, tileInfo.Height);
    3. var gameMap = m_gameMap.SpawnedEntities[0];
    4. em.AddComponent(gameMap, new Position(new float3(0f, 0f, 0f)));
    5. em.AddComponent(gameMap, default(TransformMatrix));
    6. em.AddSharedComponent(gameMap, new MeshInstanceRenderer
    7. {
    8.    mesh = mapMesh,
    9.    material = PathfindingTestBootStrap.MapTileMaterialData.Material
    10. });
    however there is no method to add a regular component type so doing something like
    Code (CSharp):
    1. em.AddComponent(gameMap, new MeshCollider())
    for example is not possible.

    I've tried filtering for the Mesh Collider within the system by adding
    Code (CSharp):
    1. public ComponentArray<MeshCollider> MeshCollider;
    to the map struct above but this ComponentArray has no setter to change the values.

    So while I can write my own detection system with rectangles and vector math, this leaves me wondering if doing components dynamically like this is not the intention or is it just due to ECS entities being too new? This assignment of the mesh collider isn't an issue for game object entities that are set up in the scene ahead of time for obvious reasons but it seems like these components are not modifiable at runtime? If anyone knows anything please let me know. It would be nice to get some insight from other people.

    For reference I am using Unity 2018.1.4f1 personal edition with entities package 0.0.12-preview.4 because this was the version I could get working on the non beta version. (I am using the non beta version just to see what is currently possible on the release build of Unity)
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    You can't add regular components (MonoBehaviours) to entities, you have to use a GameObject with the Game Object Entity component attached.

    You also may have to pre-attach an empty MeshCollider, then reset the colliders mesh property when you want to create the collider. You can then use a regular IComponentData to tag the entity as having/not having the collider created.
     
  3. TheBiter

    TheBiter

    Joined:
    May 29, 2013
    Posts:
    14
    Yea that was my assumption of how things worked based on the behaviors I noticed. I guess this is the desired intention of ECS and I'll have to write the various systems and components myself until we get more built-in IComponentData in the future.

    Thanks for the reply!