Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question How do you add a Circle Collider to an entity instantiated at runtime?

Discussion in 'Physics for ECS' started by usability_is_important, Jan 3, 2024.

  1. usability_is_important

    usability_is_important

    Joined:
    Jan 3, 2024
    Posts:
    7
    I have searched far and wide across the internet for an answer to the question above. I have scrolled through countless other posts and youtube tutorial videos. But I have found no answer.

    This should be extremely simple and straightforward, but instead it is extremely convoluted and cryptic, which has been the repeating mantra of ECS so far as I continue to wrestle with this awkwardly designed API.

    The thing I am wanting to do is so simple: just add a circle collider to an entity realtime.

    Can it be done? Can it be done in a simple way? Can it be done in a way that doesn't require hours of debugging and reading through the documentation, documentation that was written not to communicate and teach the relevant concepts described, but to haphazardly spew information at you like you're some dataset that is needing to be set with data?

    Spending hours on end trying to do the simplest of things is a major red flag. If there is no simple way to do this that is no more involved than simply adding a few lines which add the necessary collider to the entity, then that is going to be the straw that breaks the camels back, frankly.

    Systems can't just be performant, they need to be designed to be used by human beings, and they should be designed to increase the speed at which work may get done. ECS does the former, but is abysmally bad at the latter.

    EDIT: Here is the code block as was requested:
    Code (CSharp):
    1. Entity new_foe_entity = ecb.Instantiate(spawner_component.ValueRO.prefab);
    2.  
    3. ecb.AddComponent(new_foe_entity, new Most_Basic_Foe_Component
    4. {
    5.     move_dir = new float3(0, 1, 0),
    6.     move_speed = 10
    7. });
    8.  
    9. ecb.AddComponent(new_foe_entity, new PhysicsVelocity
    10. {
    11.     // Set properties for Rigidbody2DComponent
    12. });
    13.  
    14. ecb.AddComponent(new_foe_entity, new PhysicsDamping
    15. {
    16.     // Set properties for CircleCollider2DComponent
    17. });
    18.  
    19. ecb.AddComponent(new_foe_entity, new PhysicsCollider
    20. {
    21.  
    22. });
    upload_2024-1-3_11-24-39.png
     

    Attached Files:

    Last edited: Jan 3, 2024
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    908
    Don’t send screenshots of code, that’s lazy and useless. Usability is important. :) Use code tags.
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    A sample of creating bodies for Unity Physics is available here:
    https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/create-body.html
    It’s not terribly complicated when you realize that everything is there for a reason. The essence is just creating a collider blob, adding necessary transforms-related components, and filling out the values in the PhysicsSomething components. The manual setup of the archetype could be supplanted with AddComponent calls, but if you’re in control of the entity’s creation (i.e. not additively modifying an existing entity) I wouldn’t recommend it. In any event, as shown in the sample, writing wrapper methods to reusably hide away the things you do to set things up is also a possibility. However, the authors seem to have opted to keep the base API more bare and leave writing such wrappers to users. Leaving the framework’s API at this verbosity hasn’t appeared to be a blocker for most users posting here. At worst, community extensions could fill the gap.

    A variety of sample code (regularly updated as releases occur) is available here:
    https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/PhysicsSamples

    Unity Physics doesn’t have a native concept of 2D physics, so a circle collider is not applicable here. Users seem to flock to using other collider shapes (box, sphere) and constraining movement on one axis. You will only have companion objects (classic UnityEngine.Component linked to the entity, as opposed to either unmanaged or managed IComponentData) for unsupported collider types.

    Setup of a collider on an entity isn’t terribly complicated, the main part is making sure you reduce the number of intermediate archetypes that ultimately go unused after creation. Every time you call AddComponent and add a new component to the entity, changing its archetype, you end up keeping a possibly new archetype, of which the World can only track a limited number (as I recall, this should be something like 300 at least in 0.51). I would argue we shouldn’t even have AddComponent for this reason, unless basically garbage collection is implemented for unused archetypes.
     
    Last edited: Jan 3, 2024
  3. usability_is_important

    usability_is_important

    Joined:
    Jan 3, 2024
    Posts:
    7
    Thanks, you are amazing! I read you post and read the link you sent and now I have more understanding.

    This also explains another issue I was having related to this post: why my CircleCollider2D and Rigidbody2D in my prefab was not being "converted" or brought into the entity being created from it. I replaced them with 3D versions and that fixed it.

    Speaking of usability, there needs to be a warning or error when the user erroneously attempts to instantiate a gameobject prefab (like in the code above) that has 2D-only components (such as CircleCollider2D) because these 2D components can't be brought into the ECS world. Silently discarding them is very bad usability, as it creates major confusion. The truth, which is that 2D components simply will not convert over to ECS world, is something people not ought to be expected to automatically know. But thankfully, adding a warning would let them know, and that solves the issue.

    Also, I added a code block as you pointed out I should have done.
     
    Last edited: Jan 3, 2024
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    Many objects don't have an equivalent ECS component, it's not a 2D specific thing. Maybe there is/should be a warning you can turn on for sure but wanted to point this out. It's not the role of the component/feature to know this as it has no involvement in the conversion at all.

    Note that I moved your post to the correct forum for you.