Hi, I have been trying to detect the collisions between 2 entities, even copying the whole sample code into my project but nothing works. The Execute() function inside my CollisionJob doesn’t even run.
I’ve added the components to the entities, also the colliders and rigidbody.
My code:
[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(PhysicsSystemGroup))]
public partial struct CollisionDetectSystem : ISystem
{
internal ComponentDataHandles m_ComponentDataHandles;
internal struct ComponentDataHandles
{
public ComponentLookup<BoxTriggerComponent> ContactEventImpulseLookup;
public ComponentDataHandles(ref SystemState systemState)
{
ContactEventImpulseLookup = systemState.GetComponentLookup<BoxTriggerComponent>(true);
}
public void Update(ref SystemState systemState)
{
ContactEventImpulseLookup.Update(ref systemState);
}
}
private void OnCreate(ref SystemState state)
{
state.RequireForUpdate<BoxTriggerComponent>();
m_ComponentDataHandles = new ComponentDataHandles(ref state);
}
private void OnUpdate(ref SystemState state)
{
m_ComponentDataHandles.Update(ref state);
foreach(RefRO<BoxTriggerComponent> boxComponent
in SystemAPI.Query<RefRO<BoxTriggerComponent>>().WithAll<BoxTriggerComponent>())
{
state.Dependency = new CollideEventJob
{
boxTriggerComponent = m_ComponentDataHandles.ContactEventImpulseLookup
}.Schedule(SystemAPI.GetSingleton<SimulationSingleton>(), state.Dependency);
};
}
}
public struct CollideEventJob : ICollisionEventsJob
{
[ReadOnly] public ComponentLookup<BoxTriggerComponent> boxTriggerComponent;
public void Execute(CollisionEvent collisionEvent)
{
Debug.Log("Enter Job");
if (boxTriggerComponent.HasComponent(collisionEvent.EntityA)) {
Debug.Log("hit");
}
if (boxTriggerComponent.HasComponent(collisionEvent.EntityB))
{
Debug.Log("hit");
}
}
}
There isn’t much documents other than Unity’s, and most tutorial are outdated so I don’t know how to deal with this. Any help is greatly appriciated.
“BoxTriggerComponent” sounds like you may be testing trigger overlaps, in which case you want trigger events instead. Otherwise, for collisions between an ordinary collider and a rigidbody, make sure one or both colliders have providesContacts set to true, which will cause the baked physics material to have CollisionResponsePolicy.CollideRaiseCollisionEvents.
2 Likes
I’ve checked both the Provide Contacts but nothing happened.
(The name BoxTriggerComponent is just I named it like that, they’re still collisions.)
Sorry, I should have mentioned that the providesContacts baking functionality only starts with 1.3.0-exp.1. I suggest updating Entities-related packages to the latest preview, 1.3.0-pre.4.
Besides that, the entities samples repo has physics event samples. They are expected to work and can serve as a reference for how things should be set up. However, they use the custom physics authoring sample (things like PhysicsBody) which was built from the ground up to support full configuration of physics-related entities but has now been supplanted by hijacking the existing components like Collider normally used for the PhysX integration.
2 Likes
Thanks for your reply.
I asked on another forum and found the answer. Incase someone needs :
In Package Manager → Find Unity Physics. On the middle of the tab, there will be (Desciption/Version History/Dependencies/Samples).
Choose Samples → Import.
There will be a new component called Physics Shape.
Find the entities you want to have collider, remove theirs normal colliders, then add the Physics Shape.
Adjust the Collider Response to the type you want. For example: Detect 2 Colliders → Collide Raise Collision Events
While the proposed approach above works, it requires use of the collider representations from the custom physics authoring components.
The solution for built-in colliders and therefore the solution for the original problem is to use Unity Physics 1.3 and to set the “Provides Contacts” flag on the built-in collider as suggested by other users above.
As of version 1.3, this will produce the corresponding Unity Physics collision events as expected.
2 Likes
Quick additional question: Is there any other component requirement on top of a Collider? I’m trying to use this approach on a GameObject that only has a collider and no rigidbody…
Thanks
This setup will result in a static rigid body Entity, with only a PhysicsCollider ECS component.
That should work fine.
Note that if the game object is part of a hierarchy with other game objects that also only contain Colliders and no Rigidbodies, then they will all end up in a single static rigid body entity that uses a Unity.Physics.CompoundCollider within its PhysicsCollider ECS component under the hood.
1 Like