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.

Question Problem with creating PhysicsCollider and adding it to entity

Discussion in 'Physics for ECS' started by lukechodev, Jun 17, 2021.

  1. lukechodev

    lukechodev

    Joined:
    Jan 12, 2021
    Posts:
    5
    Hi, I got a problem with using PhysicsCollider with entity.
    I created PhysicsCollider by mesh info and Added the PhysicsCollider Component to entity.
    When I debug it, collider was sucessfully created , no errors, but I can't find PhysicsCollider component
    at EntityDebugger. Here's my Code & EntityDebugger window.

    Code (CSharp):
    1. public NativeArray<float3> meshPoints_list;
    2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    3. private void MakeBrick()
    4. {
    5. meshPoints_list = new NativeArray<float3>(cursorMesh.vertices.Length, Allocator.TempJob);
    6.             CreateColliderJob createColliderJob = new CreateColliderJob()
    7.             {
    8.                 Id = cursorBrickInfo._id,
    9.                 e = entity,
    10.                 points = meshPoints_list
    11.             };
    12.             JobHandle jobHandle = createColliderJob.Schedule();
    13.             jobHandle.Complete();
    14.             meshPoints_list.Dispose();
    15. }
    16. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    17. struct CreateColliderJob : IJob
    18.         {
    19.             public int Id;
    20.             public Entity e;
    21.             public NativeArray<float3> points;
    22.             public void Execute()
    23.             {
    24.                 EntityCommandBuffer commandBuffer = new EntityCommandBuffer(Allocator.Temp);
    25.                
    26.                 BlobAssetReference<Unity.Physics.Collider> collider = Unity.Physics.ConvexCollider.Create(points, default, Unity.Physics.CollisionFilter.Default);
    27.                 Unity.Physics.PhysicsCollider physicsCollider = new Unity.Physics.PhysicsCollider { Value = collider };
    28.                 if (collider.IsCreated)
    29.                 {
    30.                     Debug.Log($"physics collider validity {physicsCollider.IsValid}");
    31.                     commandBuffer.SetComponent(e, physicsCollider);
    32.                 }
    33.                 commandBuffer.Dispose();
    34.             }
    35.         }
    upload_2021-6-17_17-53-40.png
    upload_2021-6-17_17-54-38.png

    Appreciate!
     
    BeerCanAI likes this.
  2. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    Hey, I think that the commandBuffer's actions haven't executed before you deleted it, you basically just stored commands in a buffer which should be executed at a later point in time, but deleted the buffer before they got the chance to execute properly.
    To fix this, initialize a command buffer before the job, fill it with commands inside a job, and finally call
    commandBuffer.Playback(EntityManager)
    after the job completed.
     
    BeerCanAI and lukechodev like this.
  3. lukechodev

    lukechodev

    Joined:
    Jan 12, 2021
    Posts:
    5
    Thanks my hero. it works!
    btw, I have another problem with displaying collider of entities!

    upload_2021-6-19_10-43-51.png
    upload_2021-6-19_10-43-35.png

    It was just fine to add PhyscisDebugDisplayData component to entity at first, but the error occurred from 2nd entity.
    Do you happen to know why i can't display collider on editor??
     
  4. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    That component serves as a global setting that enables drawing of various physics related information, meaning that it is expected to be a singleton, and you shouldn't have 2 of them in the scene. You also shouldn't attach that component to a physics entity, since if the entity gets destroyed, it's component also goes away, and you will lose drawing functionality.
    What you should do is create a empty object in the editor, attach
    PhysicsDebugDisplayAuthoring
    component to it, and select what you want to be drawn. In order to see what is being drawn, turn on Gizmos, either in Scene or Game tabs.
    It's worth noting that functionality that draws physics information related to a single entity doesn't exist, meaning if you tick 'Draw Collider Edges', they will be drawn for all colliders that are attached to entities.
    Hope this helps!
     
    BeerCanAI and lukechodev like this.
  5. lukechodev

    lukechodev

    Joined:
    Jan 12, 2021
    Posts:
    5
    You're so professional! Thank you so much!
     
    BeerCanAI and papopov like this.
  6. jg482

    jg482

    Joined:
    Apr 5, 2018
    Posts:
    8
    Wish there would have been a note in the docs for the PhysicsDebugDisplayAuthoring component and the Physics manual about that. Took me hours to find out why nothing was drawn.
     
    lukechodev and BeerCanAI like this.
  7. gabrieloc

    gabrieloc

    Joined:
    Apr 6, 2014
    Posts:
    37
    For anyone discovering this thread and not understanding why their physics collider isn't being created - you need to also add a
    PhysicsWorldIndex
    shared component.

    Seems the best way to debug these sorts of things is by going through the sample code :(
     
    Last edited: Oct 29, 2023