#Unable to detect collision using `OverlapBox`

1 messages · Page 1 of 1 (latest)

spice cedar
#

I narrowed down the problem to a simple OverlapBox call with hardcoded values for a box in the center of the scene. I have no ideas on what might be the problem, I might be missing something. Here is the code for the test system:

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
public partial struct TestPhysicsSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
    }

    public void OnDestroy(ref SystemState state)
    {
    }

    public void OnUpdate(ref SystemState state)
    {
        var physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
        var collisionWorld = physicsWorld.CollisionWorld;

        var worldCenter = new float3(0, 0.5f, 0);
        var worldHalfExtends = new float3(0.5f, 0.5f, 0.5f);
        var worldRotation = quaternion.identity;

        var hitboxesLayer = 7;
        var hurtboxesLayer = 8;
        var outHits = new NativeList<DistanceHit>(Allocator.Temp);
        if (collisionWorld.OverlapBox(worldCenter, worldRotation, worldHalfExtends, ref outHits, new CollisionFilter { BelongsTo = (uint)hitboxesLayer, CollidesWith = (uint)hurtboxesLayer }))
        {
            Debug.Log("hit!");
        }
    }
}
delicate ibex
#

what layers are you objects in

spice cedar
#

I am drawing a rectangle using the same worldCenter, worldHalfExtends and worldRotation and the target entity is indeed inside it:

#

the cylinder has this child game object which is what I am trying to hit

delicate ibex
#

yeah so it's only in hurtboxes

#

it doesn't collide with hitboxesLayer

#

this is why i don't like box collider atm

#

and still recommend you use the previous physics shape

#

so you can properly set your layers

spice cedar
#

I have this collision matrix, shouldn't stuff in the hitbox layer collide with stuff in the hurtbox layer?

delicate ibex
#
public static bool IsCollisionEnabled(CollisionFilter filterA, CollisionFilter filterB)
{
    if (filterA.GroupIndex > 0 && filterA.GroupIndex == filterB.GroupIndex)
    {
        return true;
    }
    if (filterA.GroupIndex < 0 && filterA.GroupIndex == filterB.GroupIndex)
    {
        return false;
    }
    return
        (filterA.BelongsTo & filterB.CollidesWith) != 0 &&
        (filterB.BelongsTo & filterA.CollidesWith) != 0;
}```
#
        (filterA.BelongsTo & filterB.CollidesWith) != 0 &&
        (filterB.BelongsTo & filterA.CollidesWith) != 0;```
#

is the key here

#

it has to collide both directions

delicate ibex
spice cedar
#

I am a bit confused because I was doing some tests before with a test entity in the scene that was in the Hitboxes layer and it was colliding with this same target entity

#

but I was using a different code in another system:

foreach (var (collider, transform, entity) in SystemAPI.Query<PhysicsCollider, LocalTransform>().WithAll<HitboxTag>().WithEntityAccess())
{
    var colliderCastInput = new ColliderCastInput(collider.Value, transform.Position, transform.Position);

    if (collisionWorld.CastCollider(colliderCastInput, out var hit))
    {
        Debug.Log($"hit: {hit.Entity.ToFixedString()}, position: {hit.Position}, normal: {hit.SurfaceNormal}");
    }
}    
#

this one registers the hit if I move this entity on top of the other

delicate ibex
#

collider.Value
i assume it's using the same filter in here

#

so the filters match

#

new CollisionFilter { BelongsTo = (uint)hitboxesLayer, CollidesWith = (uint)hurtboxesLayer }
if you change hitboxes to hurtboxes

#

i'm pretty sure your code will work

spice cedar
#

changing it to BelongsTo = (uint)hurtboxesLayer, CollidesWith = (uint)hurtboxesLayer doesn't seem to have effect UnityChanThink

#

I will replace this Box Collider with a Physics Shape component

delicate ibex
#

FilterA and FilterB will by the cylinder and the overlap sphere in this case

spice cedar
#

new game object, I changed the object layer back to Default and set the Collision Filter to what I think makes sense in my mind ThinkDerp

#

still doesn't seem to collide

delicate ibex
#

because this now looks fine to me =S

spice cedar
#

oh wait

#

I need to revert the change of BelongsTo back to hitboxesLayer

#

nothing UnityChanBugged

#

I think this method is returning true so the filters might not be the issue anymore

spice cedar
delicate ibex
#

if you break point here and it's returning true then the overlapbox should be returning true

#

not sure what's up size except size/position being wrong then

#

if you use CollisionFilter.Default does it return?

spice cedar
spice cedar
#

uh but it is kinda weird, let me look into it further

#

ah yeah it is colliding with everything now

#

so I need to rethink about the previous filter

spice cedar
#

ok I did some more tests with different values for the filter

#

for the OverlapBox, I kept the BelongsTo as 7 (hitbox layer) and increased the CollidesWith from 0 to 11

#

when the value of CollidesWith was 1, 3, 5, 7, 9 or 11 it would trigger the hit with everything in the scene that entered the box

#

I can't really make sense of this so I guess I am gonna sleep PaissaSleep

#

almost 5 hours of trying to make one thing hit the other

#

I will try again tomorrow, thank you for the help so far UnityChanwow

delicate ibex
#

OH your layers are wrong

#

it's not 7 or 8

#

it's 1 << 7

#

it's a bit field

#

7 is
0000_0111
you want
layer 7 is
1000_0000

#

sorry should have spotted that

spice cedar
#

oooh it worked grubhappy

#

now that you mentioned it it all makes sense, we can select multiple layers for the filter in the editor so the input is also a layer mask