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

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:

Entity new_foe_entity = ecb.Instantiate(spawner_component.ValueRO.prefab);

ecb.AddComponent(new_foe_entity, new Most_Basic_Foe_Component
{
    move_dir = new float3(0, 1, 0),
    move_speed = 10
});

ecb.AddComponent(new_foe_entity, new PhysicsVelocity
{
    // Set properties for Rigidbody2DComponent
});

ecb.AddComponent(new_foe_entity, new PhysicsDamping
{
    // Set properties for CircleCollider2DComponent
});

ecb.AddComponent(new_foe_entity, new PhysicsCollider
{
  
});

Don’t send screenshots of code, that’s lazy and useless. Usability is important. :slight_smile: Use code tags.
https://discussions.unity.com/t/481379

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.

1 Like

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.

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.