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. Dismiss Notice

Add MeshCollider at runtime. Not work.

Discussion in 'Physics' started by DKAvvy, Oct 2, 2020.

  1. DKAvvy

    DKAvvy

    Joined:
    Oct 2, 2020
    Posts:
    3
    Hi! A try add MeshCollider at runtime. After PhysicsCollider existing in Entitiy, but colider looking as epmty vithout vertices and triangles data. Collision events with this Entity not working. Native arrays lenght not equals 0. Lenght equals array in mesh data.


    Code (CSharp):
    1.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    2.         {          
    3.             dstManager.AddComponentData(entity, new Parent());
    4.             dstManager.AddComponentData(entity, new PostRotation());
    5.             dstManager.AddComponentData(entity, new Scale {Value = 1});
    6.             dstManager.AddComponentData(entity, new LocalToWorld() {Value = float4x4.TRS(10000f, quaternion.identity, 1)});
    7.             dstManager.AddComponentData(entity, new LocalToParent());
    8.             dstManager.AddComponentData(entity, new Translation());
    9.             dstManager.AddComponentData(entity, new BlockTypeComponent {Value = _blockType});
    10.             dstManager.AddComponentData(entity, new BlockPosInGridComponent{InGridIndex = -1});
    11.             conversionSystem.DeclareLinkedEntityGroup(gameObject);
    12.  
    13.             var mesh = dstManager.GetSharedComponentData<RenderMesh>(entity);
    14.             var vertices = mesh.mesh.vertices;
    15.             var triangles = mesh.mesh.triangles;
    16.  
    17.             NativeArray<float3> nativeVertices = new NativeArray<float3>(vertices.Length, Allocator.Persistent);
    18.             NativeArray<int3> nativeTriangles = new NativeArray<int3>(triangles.Length, Allocator.Persistent);
    19.  
    20.  
    21.             LayerMask msk = LayerMask.GetMask("Box");
    22.  
    23.             var filter = new CollisionFilter
    24.             {
    25.                 BelongsTo = (uint)msk.value,
    26.                 CollidesWith = ~0u,
    27.                 GroupIndex = 0,
    28.             };
    29.  
    30.             BlobAssetReference<Collider> colliderCopy = Unity.Physics.MeshCollider.Create(nativeVertices, nativeTriangles, filter);
    31.  
    32.             dstManager.AddComponentData(entity, new PhysicsCollider {Value = colliderCopy});
    33.         }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    The DOTS physics forum is here.
     
  3. DKAvvy

    DKAvvy

    Joined:
    Oct 2, 2020
    Posts:
    3
    Thanks