#Feeding data from Monobehaviours to ECS to handle collision events

1 messages · Page 1 of 1 (latest)

night birch
#

Hey there,

So i'm fairly deep into my project, and I want to make what will be a fairly big optimization which is to offload all collision detection to ECS.

My theory is to emulate colliders and their positions in ECS, then whenever there is a collision event I can trigger monobehaviour code.

To create the entity/component data, I have a script i attach to any gameobject that needs collisions to be detected. This script creates the entity and attaches a "collsionTag" component to it, as well as a physics collider, local transform and physics mass (as I believe these physics componets are required to detect collisions). I then also update the entities "local transform" position via the monobehaviour script to align with the gameobject.

Then, in a ISystem, I run a "ICollisionEventsJob" and run my business logic for detecting the correct collisions, where eventually the calling code to monobehaviour functions will be. But i can not for the life of me figure out why my collisions aren't being detected. I've looked into the documentation and it hasn't helped, and also looked at a number of different tutorials and tried to copy what they're doing to no avail.

I'm aware what I am doing might be unconventional, but it would be great if someone could help or point me in the right direction.

Thank you.

idle cipher
#

It would be much more helpful if you have a reproduction project. Because the issue is not that simple and the way you describe it doesn't provide enough information on how you're trying to do that. (At least from my POV.)

night birch
idle cipher
#

learing material for ECS is incredibly sparse.

@night birch Do you mean ECS in general or Unity Entities? I suppose you are going to use Unity Entities so did you read the manual for Unity Entities? Do you have any specific question? It might be better to start from that instead of jumping right into the complex issue. (Maybe it is now an issue because you have yet understood how Unity ECS should work?)

night birch
#

Understandable, this is just how I know I learn best.

I guess my question is if my approach is valid (feeding data into entities/components and driving the collision detection via a system). This will allow me to know whether or not struggling with this for a week or 2 is worth it.

weary mango
#

I think it's possible, and there are some assets that work in this way for compatibility with mono land
I assume you have an issue with regular PhysX, to resort to entities?

#

the collision detection issue sounds unrelated to your approach

#

it makes me think there's perhaps a layer issue, or a collider setup issue

one particularity is that colliders in Unity.Physics (which I assume is what you are using?) are blob assets, and not stored on the entity directly; normally these are created by the baking process

jade veldt
#

I think what you want is the opposite. You want Dots Physics to work with monobehaviors. I think the easiest way would be to create entities at runtime to mirror your colliders. Then you can use you monobehaviors to query those entities and move accordingly

night birch
#

so yes, I am using Unity.Physics. From what I currently understand, it is'nt a layer issue, it could dfeinitely be a collider setup issue. I am creating this game in 2D, so I'm unsure if that could be causing issues.

Currently the script that "Authors" the entity (that is attached to the Monobehaviour that I want to emulate in ECS) creates the entity and it's components in it's start function, by calling "CreateEntity" and "AddComponentData" for relevant components.

I do know about the alternate nested "Baker" class I can implement, but for unknown reasons this doesn't work.

I can see entities being created in the entities hierarchy, and the system is also created, but for some reason it's not triggering any collision events.

night birch
night birch
jade veldt
night birch
weary mango
#

PhysX is quite optimized behind the scenes as well, so I'm wondering whether you see an improvement with Unity.Physics?

Baking only works at editor-time, there is no runtime baking process. You bake the entities beforehand (e.g. store a prefab entity in a component), which you can then spawn at runtime. So it's a different thing, you need a collider on your prefab entity for this to work as intended.

weary mango
jade veldt
#

It's been a while since I've used Dots. I'm pretty sure all you need to add at runtime is a physics shape, physics mass, and physics velocity

viral leaf
#

kind of going to be an uphill battle if you really want to do this op.
basically it used to be much easier to do this before when conversion was runtime(before baking replaced everything), but now if you want to do this you will either have to create your own runtime ecs collider creation code, or move anything touching physics to subscenes, where native 2d doesnt exist.
baking(bakers and bakingsystems) only happens in subscenes, in the editor.

any physics entity you want collisions from either needs the collision event setup via the physicsbody authoring setup(found in physics samples of the physics package), or to be done via code which im not sure if the docs even cover tbh. https://discussions.unity.com/t/how-to-set-collisionresponsepolicy-inside-the-baking/1638820/2

you will also have to manage the lifetime(basically disposal) of any blobs yourself if you dont use subscenes

jade veldt
#

oh dang. I didn't realize it changed that much. Can you still have the subscenes leave the baked gameobjects in the scene?

night birch
# viral leaf kind of going to be an uphill battle if you really want to do this op. basically...

yeah, this sounds like a headache, but it should be possible.

From my understanding, I am currently creating an ecs collider and the entity in my Monobehaviour script i attach to the gameobject I want to emulate in ecs. I do this via the Start function with the following block of code:

Entity = EntityManager.CreateEntity();

var sphere = Unity.Physics.SphereCollider.Create(new Unity.Physics.SphereGeometry
{
Center = float3.zero,
Radius = _colliderRadius
}, CollisionFilter.Default);

EntityManager.AddComponentData(Entity, new PhysicsCollider { Value = sphere });
EntityManager.AddComponentData(Entity, PhysicsMass.CreateDynamic(
MassProperties.UnitSphere, 1));

night birch
viral leaf
#

will need a PhysicsWorldIndex(shared component), PhysicsVelocity, possibly a PhysicsDamping? also add a material with raise events flag to it in the geometry creation(like in the link). I honestly find it hard to remember off the top of my head, its far easier in many cases to just make a simple subscene, add a gameobject with the PhysicsBodyAuthoring and PhysicsShapeAuthoring and see what they bake to and see what you need because depending on your needs sometimes the components can change.
also theres stateful events sample code in https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/PhysicsSamples/Assets/6. Events/Scripts/Stateful if you havent already seen those its well worth spending some time checking out all if it